forums

TNT Basic Forums > Programming Q&A
Sprite Col with Map Objects
< Last Thread     Next Thread >
Author
Thread        Post A Reply

10-25-2009 20:41

Posted by:
Destro

Location:
LBC (langley British Columbia)

Find more posts by Destro

I'm looking through the backlogs of threads for this, but incase I don't find it:
My RPG code is layed out following the map tutorial on this site, so I've got a for loop for objects.
I've also got two spells, one is fire, one is cutter. When the fire sprite collides with the tall grass object, I want the object involved in the collision to show a different sprite image, my picture of the tall grass burnt to a crisp. Likewise with the cutter, a cut grass object should show cut grass, but the way I'm doing it turns ALL the grass to crispy grass.

for n=0 to objNum-1
objType=map object type (n)

if objType=0
'
end if
if objType=1 'tall grass
set sprite priority n+2,(oY[n]-ymap)-10
if Aflame=true
sprite n+2,oX[n]-xmap,oY[n]-ymap, 127 'burnt grass
else if Chopped=true
sprite n+2,oX[n]-xmap,oY[n]-ymap, 126 'chopped grass
else
sprite n+2,oX[n]-xmap,oY[n]-ymap, 55 'normal grass
end if
end if

collisionFire=sprite col (fInc,0 to 10000)

if collisionFire > 0 and sprite image (collisionFire)=55 'tall grass
for b=0 to (maxAflame-1)
fireStartX = sprite X (collisionFire)
fireStartY = sprite Y (collisionFire)
fireDuration=fireDuration-200
if fireDuration<-200 then fireDuration=-200 'maximum 'duration, resets to this each time a collision occurs
fireDuration=fireDuration+5 'counts up by 5
if fireDuration>105 then fireDuration=105 'don't count higher 'than 105
if fireDuration<100 'if sprite 14 isn't completely invisible...
sprite 14, fireStartX, fireStartY, fireStartani
set sprite priority 14,999999
set sprite transparency 14,fireDuration
else
sprite off 14
end if
next b
end if
collisionCutter=sprite col (cInc,0 to 10000)
if collisionCutter > 0 and sprite image (collisionCutter)=55
Chopped=true
end if

I realize this is all wrong, that this checks for collisions with tall grass the image, not the object. Could someone help me put this in the CORRECT for loop so that more than one object can be burnt onscreen and independantly of the others of its kind. I can't wrap my mind around this one.

10-25-2009 22:29

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Hi again Destro...

I'd forget objects and interrogate the map tiles directly when it comes to static stuff like grass. Objects are generally used for start locations of objects, or pick-ups / power-ups etc. things which can move or be moved.

In your case, I'd make your 'long grass' sprite part of your map tiles image, and also add the burnt and cut sprites. When a spell is cast, check which map tile it's been cast on (with 'Map Tile()'), and use 'Set Map Tile()' to change the image (and any subsequent behaviour involved with crossing that tile) dependant on which spell was cast.
This approach will get rid of a load of sprites, freeing up system resources and making the game run faster.

Another quick tip when it comes to map tiles:

In your map tiles image, try and keep all related images together (reading from top left). TNT reads images left to right, top to bottom numbering sequentially as it goes. If all of your 'clear' tiles occupy the first slots (clear tiles are generally the most common type) then you can check whether a move is legal with one single condition:

if Map Tile(spriteX, spriteY, mapLayer) < [last clear tile ID] then [move is possible]

If your total obstructions come next (second most popular type) you can check for an invalid move with another single command (as you already know that it's not a 'clear' tile). By placing the most common tile types first you can speed the collision detection process by filtering out the less likely options. The less likely something is to happen, the further down the tile list it goes... Simple this game design isn't it?
There are even faster ways to deal with map collisions and rendering, just let me know if you're interested (it takes a bit of explaining!), but get this working first as you'll need to understand these concepts in order to move on.

Happy coding!

Danny (nod the mod)

10-26-2009 02:04

Posted by:
Destro

Location:
LBC (langley British Columbia)

Find more posts by Destro

thanks I will try it for some uses, but I'd like to keep tall grass and tall trees as sprites so that you can walk in front or behind them, you can't do that with map layers or tiles. Is it possible to assign sprites to map tiles based on their tile type wherever they occur like secondary map objects?

10-26-2009 09:32

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Compromise...

I forgot the tree issue - my bad!

OK, writing games is all about compromise. It's a trade off between what you want to happen and what you can get away with at runtime. Turning every vertical obstruction in your game into a sprite when you load the level is going to be very CPU intensive, but they need to be sprites for the reasons you outlined. You need a compromise. The question is "When do I need them to be sprites?", and the answer has to be "only when a character is walking behind them".
You can use your map for collision detection, as I suggested in the other thread, and also check the next tile down. This will be the thing the player character is just about to walk behind. If this tile indicates a vertical obstruction then you place a temporary sprite over the tile using the 'priority=y coordinate' method, and if it's clear then you can remove the sprite (if present).
This is only a skeletal theory and would need to tailored to your system, you would probably need to display three temporary sprites for example (one directly below, and one to either side to cover the transition between tiles), but you could then walk through a field of long grass using only three additional sprite images. You get your collision detection and your display integrity with the minimum of overhead.

Any more problems just let us know...

Danny (nod the mod)

10-26-2009 18:54

Posted by:
Destro

Location:
LBC (langley British Columbia)

Find more posts by Destro

thank you for all that, and yes I agree compromise is what it's about. I've compromised a fair bit and now there's code everywhere that was meant to do more than what it does, so I could simplify a lot to get more efficiency...
For example my spells are for loops of many potential spell sprites that fly where you point the mouse, but I couldn't get all the sprites to collide, only one in each swarm, so now I have only one spell sprite that animates to explode into many little ones, and they are all the same collision. But, if I can run this past you, the way I have it now a fire is supposed to start on the spot of the collision:

fireDuration=fireDuration+5
if fireDuration<100
set sprite transparency 14,fireDuration
sprite 14, fireStartY, fireStartX, fireStartANI
else
sprite off 14
end if

if collisionFire > 0 and sprite image (collisionFire)=55'tall grass
fireStartX= Sprite X (collisionFire)
firesStartY= sprite Y (collisionFire)
fireDuration=fireDuration-200
end if

It works, but the flames jump from object to object upon collision and I'm trying to get it to add another sprite 14 to each new collision spot, possibly with independent fireDurations...

10-26-2009 22:39

Posted by:
Destro

Location:
LBC (langley British Columbia)

Find more posts by Destro

tilefball = map tile ((fX)/16, (fY)/16,0)
if tilefball=195
set map tile (fX)/16, (fY)/16,0,196
end if

...and what's wrong with that too?

10-26-2009 23:07

Posted by:
Destro

Location:
LBC (langley British Columbia)

Find more posts by Destro

though fireballs seem to be part of the viewport only, so does this prevent them from touching tiles or throw their coords off?
I'm really stuck. Everything I do is a battle and I'm losing

10-27-2009 07:00

Posted by:
Destro

Location:
LBC (langley British Columbia)

Find more posts by Destro

clarifying

Sorry, was getting frustrated there. Found out, I was omitting...
if sprite image (n+2)=-1 then objType=-1

...from the objects section, where turning an object sprite off upon collision turns it off permanently.
So now the fireball shoots, burns up the plant (turns off the plant sprite and leaves a burning sprite 14 in it's place), gets fuel from the plant to keep burning longer (an otherwise short firespell becomes deadly when standing next to plentiful foliage) and follows the mouse until it has exhausted its fuel.

Would still really appreciate help with getting more fires happening onscreen. I think I did everything you said otherwise to optimise the tiles, but my tile collisions aren't working for spell sprites, just the hero bumping into walls and stairs.

10-27-2009 22:07

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Quick question...

I've only had a brief look at your code (it's been one of those days!), but one thing does stand out straight away, and that's this sprite 14 thing. Are you trying to implement all the fire animations with one sprite?
This, I'm afraid, is doomed to failure as there will only ever be one sprite 14 (forgive me if I'm telling you something you already know, but as I said it's been a long day), you can't have multiple "Sprite 14s".

People have reported incompatibilities between map tiles and viewport based sprites in the past, but as I use maps differently I have never encountered this problem. There is a pretty simple 'work around' though.

It's pretty tricky to see exactly what's going on from a limited source listing, and without being able to see the problem first hand. If you want, you can mail over a copy of your project and I'll take a look at it for you (you will need to either zip or stuff it though). My mail address is 'cryptically encoded' to the left...

'Till next time...

Danny (nod the mod)

11-01-2009 23:36

Posted by:
Destro

Location:
LBC (langley British Columbia)

Find more posts by Destro

Thanks for the offer, I'm trying to finish one tileset before I send it to you so you get a better idea of how it's supposed to feel.
I don't plan on setting up my email through this computer and when I click on your name it wants me to do the setup, could you email me your email?

11-01-2009 23:44

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Not without your e-mail ;)

Try nods at allnodcoms dot com...

Danny (nod the mod)

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.