Replace a sprite by another before deletion

I want to replace an animated sprite by another before it gets deleted. I don’t know how I could do it.
I’m basing my game upon the side Scroller tutorial and in it, when the stars collide with the spaceship, they play a particles effect then gets deleted.
In my game, I want to use another animated sprite instead of a particle effect.
In the side scroller tutorial, the code that checks collision calls the particle to play then deletes the object:

    if message_id == hash("collision_response") then
        particlefx.play("#pickup")
        go.delete()
    end

I didn’t really understood how the particles effect could even play since it was attached to the same game object that got deleted. I might miss some information about deletion process…

Anyway, I need some hints on how I could do this swiching sprite thing.

The particle system is designed to be “fire and forget”, just for that particular reason (it’s a very common use case)

As for spawning another sprite, I suggest you spawn another gameobject/collection from a factory/collectionfactory?

2 Likes

Ok now I better understand that peculiar behavior.

Using a factory then? Thank you for the hint.
If the automatic animation has ended (no looping), is there a way to detect that in order to delete it. I tried this code but it didn’t worked:

function on_message(self, message_id, message, sender)
	if message_id == hash("animation_done") then
		go.delete()
		print(sender.."deleted")
	end
end

It seems that animation_done is only

sent to the sender of a play_animation message when the animation has completed.

So it doesn’t work for an animation that plays automatically.
Maybe check the current frame value then?

This seems to work but I don’t find it very elegant:

function init(self)
	msg.post("#sprite", "play_animation", {id = hash("sparks")})
end

function on_message(self, message_id, message)
	if message_id == hash("animation_done") then
		go.delete()
		print("deleted")
	end
end
1 Like

Don’t forget that you can do:

function init(self)
    sprite.play_flipbook("#sprite", "sparks", function()
         go.delete()
    end)
end
1 Like

Nice! Thank you.^^