Simple method of sprite "autodestruct", ie for a small explosion

Hi all, thanks again for any help-

I've managed to get a simple sprite to pop up when an "enemy" is destroyed is my 'lil test game, however now looking for how to make it "autodestruct".

Currently, my code goes somewhat thusly:

...
(if projectile hits an enemy...)
NewEnemyExplosion(event.object1)

...

function NewEnemyExplosion(theEnemy)
theExp = sprite.newSprite( expSet)
theExp.x = theEnemy.x
theExp.y = newEnemy.y
theExp:play()
end

This works fine for dropping in explosions where the enemy goes kaboom, but obviously just keeps playing forever. Any tips on a simple method of making the sprite autodestruct once it's finished the loop?

Thanks!
gw

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
local function removeExplosion(event)
    if event.phase == "end" then
        event.target:removeSelf()
        event.target = nil
    end
    return true
end
 
local function newExplosion(x,y,size)
    local boomInstance
    boomInstance = sprite.newSprite(boomSpriteSet)
    boomInstance:prepare("boom")
    boomInstance.x = x
    boomInstance.y = y
    boomInstance:addEventListener( "sprite", removeExplosion )
    boomInstance:play()

Hi robmiracle, thanks for your fast response, just getting back to this-

Unfortunatly that isn't working for me, though I'm probably just missing something simple. I get no errors, but the sprite never stops. I've confirmed that by removing the "if-then" from your "function RemoveExplosion", it does kill the sprite, so the issue is that "event.phase" never equals "end".

Anything I might have missed? Here is my code, just changed a few names:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function RemoveExplosion(event)
        --if event.phase == "end" then
                event.target:removeSelf()
                event.target = nil
        --end
        --return true
end
 
function NewEnemyExplosion(theEnemy)
        local expInstance
        expInstance = sprite.newSprite(expSet)
        expInstance.x = theEnemy.x
        expInstance.y = theEnemy.y
        expInstance:addEventListener("sprite", RemoveExplosion)
        expInstance:play()
end

Ah, I see- my sprite never "ends", it only loops indefinitly. I changed it to "loop", and that works.

For the future (and now), where would I tell my sprite not to loop like this? I see a loop param in the "sprite.add" code, but not sure how do it with my setup:

1
2
3
4
--|Creation and storing of basic sprite explosion
expSheet = sprite.newSpriteSheet( "explosionSprite_gradius.png", 96, 96 )
expSet = sprite.newSpriteSet(expSheet, 1, 4)
--|

I didn't include my sprite init code:

1
2
3
boomSpriteSheet = sprite.newSpriteSheet("exp2_alpha_1.png", 61, 61)
boomSpriteSet = sprite.newSpriteSet(boomSpriteSheet,1,16)
sprite.add(boomSpriteSet, "boom", 1, 16, 500, 1)
views:1498 update:2011/10/9 22:34:50
corona forums © 2003-2011