Sprite animation loops constantly? Need it to stop!
-
What are you setting your sprite animation sequence to? I've not used sprite animations yet, but consider this...
// 0 to 50 at 60fps setSpriteAnimation( enemy, 0, 50, 60 ) if getSpriteAnimation(sprite) == 64 then // Will never trigger removesprite(sprite) endif
Sorry, if it's obvious, but just check that your animation can actually get to 64 and that you haven't told it otherwise
Beyond that i don't see anything desperately wrong with what you're doing in principal but like I say, I've not actually used sprite animations myself yet. I cheat, all of my enemies just rotate :)
-
So, the other thing is that once you've removed your sprite I'm pretty sure you're quickly going to find that things crash because you can't loop over a non existent entity. I'm sure there are other ways round this but the simplest is to create a sprite that you never use, like so:
nullSpr = createSprite() // Do whatever you need to do // In your loop you can now say for n = 0 to len(enemies) loop if enemies[n] != nullSpr then // do all your stuff, including when you're done with it... removeSprite(enemies[n]) enemies[n] = nullSpr endif repeat
Hopefully I've got that the right way round. Important is to always remove your sprite before you assign it to null sprite, or indeed assign it to a newly created one. Otherwise you will end up breaking Fuze sooner or later (things get messy internally right now in that scenario but will get fixed!). The other nice thing about actually removing the sprite rather than just moving it offscreen or anything like that is that you then no longer need to care about it in terms of collision detection or anything like that and I tend to create functions like
createPlayer()
,createEnemy()
so when I need to reset the level it's easy because I know I've removed all the old sprites so I just call those functions to create new ones and those functions can come with all the default properties for the player / enemy, like so...function createPlayer() p = createSprite() p.image = img // image loaded previously setSpriteLocation(p, gWidth() / 2, gHeight / 2) // and all your other usual sprite creation commands p.lives = 3 p.speed = 10 // whatever other custom properties your player object might want return p
-
@Martin yeah i need to start doing this delete sprite its just taking the plunge
-
Just a side note
The following code "should" do exactly the same and not use a sprite slot
// Do whatever you need to do // In your loop you can now say for n = 0 to len(enemies) loop if enemies[n] != false then // do all your stuff, including when you're done with it... removeSprite(enemies[n]) enemies[n] = false endif repeat
I just realised why this doesn't work and I am going to try to get it addressed in a future patch as to me it is very odd behaviour and 100% cannot be "by design"
You cannot compare a sprite object to anything other than another sprite object, so setting it to false , null, void, 1, "nothing" or anything else will not work... hence the requirement for an empty sprite
-
Thanks for all the replies, I'll check them out today!
Just had a thought laying here after just waking up, and that is;
// Do whatever you need to do
// In your loop you can now say
for n = 0 to len(enemies) loop
if enemies[n].sprite != visible then
If explosionSprite[n] frame count>60
removeSprite(enemies[n])
Endif
endif
repeatAs only destroyed aliens will ever be exploding?...
Anyways, thaanjs again. Will update how it's gone :)
-
-
@SwitchedOn sorry I did not look up the docs, but what is the variable visible? Is this pseudo code?
-
@SwitchedOn
As soon as i hit an enemy, i put it outside the screen area and make it static.sprite.y = -10000// shoud be enough to avoid any collision with other sprites setSpriteSpeed( sprite, {0.0} ) // i don't want the enemy to come back in the screen
I don't delete the sprite,
-
@spikey said in Sprite animation loops constantly? Need it to stop!:
@SwitchedOn sorry I did not look up the docs, but what is the variable visible? Is this pseudo code?
Yea it is, but its a 'get'
getSpriteVisibility(spriteName)
Can also use a 'set'
setSpriteVisibility(sprintName, Boolean)
-
@doumdoum said in Sprite animation loops constantly? Need it to stop!:
@SwitchedOn
As soon as i hit an enemy, i put it outside the screen area and make it static.sprite.y = -10000// shoud be enough to avoid any collision with other sprites setSpriteSpeed( sprite, {0.0} ) // i don't want the enemy to come back in the screen
I don't delete the sprite,
Whilst this works, I wouldn't approve of this as a best practice way of doing things.
-
@MikeDX said in Sprite animation loops constantly? Need it to stop!:
@doumdoum said in Sprite animation loops constantly? Need it to stop!:
@SwitchedOn
As soon as i hit an enemy, i put it outside the screen area and make it static.sprite.y = -10000// shoud be enough to avoid any collision with other sprites setSpriteSpeed( sprite, {0.0} ) // i don't want the enemy to come back in the screen
I don't delete the sprite,
Whilst this works, I wouldn't approve of this as a best practice way of doing things.
Yep, could end up being a bit of a memory hog - plus as they are still active to a degree, could cause issues in detections etc.
-
In practice, in a space invader/galaxian game, i have a constant number of enemies (50 max).
I recycle the sprites on the next stage, only by changing properties.(ecolo coding)
Since the sprites are static and far away, they don't collide with bullets, Ships etc.
I do the same with bullets, explosions.
I admit it's a rough method, but it works well.
Give me stack and list, and i'll make it better.