Play Sprite, then destroy

Adding finishing touches to my entry to the Techority 48 hour challenge, and I thought I want to add a splash after water balloon object has been hit.

[lua]elseif self.name == “coinShot” and event.other.name == “balloon” and gameIsActive then
score = score + 3
scoreText.text = "score: "…score
scoreText.x = 250
audio.play(water_balloon_sound)
event.other.currentFrame = 2
timer.performWithDelay( 1000, table.insert(toRemove, event.other), 1)[/lua]

In this case if I remove the timer, the balloon turns into the splash when hit, but doesnt get destroyed. So I added the timer, which I want to destroy the balloon after 1 second. But with the timer added, it doesn’t wait, and it destroys the balloon immediately without switching to the second fram of the sprite sheet.

Is it not possible to do the following?
[lua] timer.performWithDelay( 1000, table.insert(toRemove, event.other), 1)[/lua]
Thanks in advance :slight_smile: [import]uid: 7116 topic_id: 22437 reply_id: 322437[/import]

Hey AlexGreene, using delays to remove stuff can be iffy, because it’s possible that events will happen out of sync. Use a callback if possible.

Here’s how I play a sprite animation and delete it afterwards. The key line is: explosionInstance:addEventListener(“sprite”, spriteAnimationHandler) and the function that that line calls.

[code]
– ANIMATION HANDLER
spriteAnimationHandler = function(evt)

–remove animation after completion
if “end” == evt.phase then

–remove this listener
evt.target:removeEventListener(“sprite”, spriteAnimationHandler)

–remove the sprite
evt.target:removeSelf()

–set sprite to nil to reclaim memory
evt.target = nil

end
end

– PLAY ANIMATION
playAnimation = function(evt)

–data for explosion sprite
local explosion = require(“explosion”) --explosion.lua defines spritesheet data
local explosionData = explosion.getSpriteSheetData()

–create sprite from data
local explosionSpriteData = sprite.newSpriteSheetFromData(“explosionspritesheet.png”, explosionData)

–create instance of spriteset
local explosionSpriteSet = sprite.newSpriteSet(explosionSpriteData, 1, 12)

–add sprite
sprite.add( explosionSpriteSet, “explode”, 1, 12, 250, 1 ) – play 12 frames every 250 ms

–create instance of sprite
local explosionInstance = sprite.newSprite( explosionSpriteSet )


–add listener to take care of sprite when its done animating

explosionInstance:addEventListener(“sprite”, spriteAnimationHandler)
–prepare clip and play it
explosionInstance:prepare(“explode”)
explosionInstance:play()

end

[/code] [import]uid: 120 topic_id: 22437 reply_id: 89486[/import]

I’m not sure how to implement this solution. My sprite sheet isn’t actually playing. It simply where the first frame is a balloon. The balloon is falling and when the player shoots at it, it should switch to the second frame and then remove itself after 1 second. How would I delay 1 second without using a timer?
Thanks [import]uid: 7116 topic_id: 22437 reply_id: 89529[/import]

It sounds like you could use a timer but in the function just check the obj exists before removal.

if myobj then
myobj:removeSelf()
end

Peach :slight_smile: [import]uid: 52491 topic_id: 22437 reply_id: 89552[/import]

Ah, my bad Alex - I thought you were playing a sprite animation for a 1 second duration. Hopefully peach’s solution worked for you! [import]uid: 120 topic_id: 22437 reply_id: 89578[/import]