removeSelf and transitions

I have some enemy objects which fire weapons on timers. The weapons are fired while the enemy objects are moving with transition.to.

I am experiencing a problem where if the enemies are destroyed, the weapons are still fired. Whats more strange, is that the weapons are set to fire from the enemies x and y and after they are destroyed the weapons are fired from the x and y where the enemy would be during the transition if they weren’t destroyed.

I am using transition.to for all of the movement, timers to activated the weapon firing, and removeSelf() for when the enemy is destroyed by the player.

Has anyone else ran into this issue, or have an idea for a solution? [import]uid: 102017 topic_id: 18161 reply_id: 318161[/import]

It could be easier to diagnose the problem if you posted your code.

Regards,
Jordan Schuetz
Ninja Pig Studios [import]uid: 29181 topic_id: 18161 reply_id: 69481[/import]

| [import]uid: 29181 topic_id: 18161 reply_id: 69482[/import]

Here is an example. In this version, the enemyObject is removed on tap instead of on a collision with the playerObject bullets; but the result is the same.

[lua]local playerObject = display.newCircle (160, 450, 25)

local enemyObject = display.newCircle (-50, 0, 25)

function enemyShoot()
local enemyBullet = display.newCircle (enemyObject.x, enemyObject.y, 3)
transition.to (enemyBullet, {time = 2000, x = playerObject.x, y = 500})
end
timer.performWithDelay (1000, enemyShoot, 6)

function transitions()

transition.to (enemyObject, {time = 2000, x = 100, y = 200})

function transition2()
transition.to (enemyObject, {time = 2000, x = 200, y = 100})
end
timer.performWithDelay (2000, transition2, 1)

function transition3()
transition.to (enemyObject, {time = 2000, x = 340, y = 0})
end
timer.performWithDelay (4000, transition3, 1)
end
transitions()

function enemyDeath(event)
enemyObject:removeSelf()
end
enemyObject:addEventListener (“tap”, enemyDeath)[/lua] [import]uid: 102017 topic_id: 18161 reply_id: 69590[/import]

Change line 9 to this;
[lua]shootTimer = timer.performWithDelay (1000, enemyShoot, 6)[/lua]

Then in your enemyDeath function add this;
[lua]timer.cancel(shootTimer)[/lua]

That should do the trick.

Peach :slight_smile:

[import]uid: 52491 topic_id: 18161 reply_id: 69656[/import]

Well that works. Except I should have been more clear about my situation, because I still experience the bug in my real game’s code.

Are you aware of a fix for this if everything is contained in a function, say if you’re spawning enemies? Or perhaps a better way to handle this in general?

Here is a better example of what I am trying to do. Sorry.

[lua]local playerObject = display.newCircle (160, 450, 25)

function spawnEnemy()
local enemyObject = display.newCircle (-50, 0, 25)

function enemyShoot()
local enemyBullet = display.newCircle (enemyObject.x, enemyObject.y, 3)
transition.to (enemyBullet, {time = 2000, x = playerObject.x, y = 500})
end
shootTimer = timer.performWithDelay (1000, enemyShoot, 6)

function transitions()

transition.to (enemyObject, {time = 2000, x = 100, y = 200})

function transition2()
transition.to (enemyObject, {time = 2000, x = 200, y = 100})
end
timer.performWithDelay (2000, transition2, 1)

function transition3()
transition.to (enemyObject, {time = 2000, x = 340, y = 0})
end
timer.performWithDelay (4000, transition3, 1)
end
transitions()

function enemyDeath(event)
enemyObject:removeSelf()
timer.cancel(shootTimer)
end
enemyObject:addEventListener (“tap”, enemyDeath)
end

timer.performWithDelay(1000, spawnEnemy, 3)[/lua] [import]uid: 102017 topic_id: 18161 reply_id: 69746[/import]

You would also need to cancel your timers as well as transitions when removing the enemies.

index your timers and transitions.

Example:

[code]
local tmr = timer.performwithdelay(100, whatever)
local tran = transition.to(whatever, {alpha = 0})

–Then you can cancel them like

timer.cancel(tmr)

–etc
[/code] [import]uid: 84637 topic_id: 18161 reply_id: 69781[/import]

I tried your method, thanks for the help.

When I use this method, the same problem happens along side a new one where the transitions, their orders, and times are interpreted by additionally spawned objects incorrectly.

Not sure what I’m doing wrong, but here is the modified code in case you are able to spot something.

Also, I have tried adding ‘local’ to the timers and transitions with no result.

[lua]local playerObject = display.newCircle (160, 450, 25)

function spawnEnemy()
local enemyObject = display.newCircle (-50, 0, 25)

function enemyShoot()
local enemyBullet = display.newCircle (enemyObject.x, enemyObject.y, 3)
transition.to (enemyBullet, {time = 2000, x = playerObject.x, y = 500})
end
shootTimer = timer.performWithDelay (1000, enemyShoot, 6)

function transitions()

t1 = transition.to (enemyObject, {time = 2000, x = 100, y = 200})

function transition2()
t2 = transition.to (enemyObject, {time = 2000, x = 200, y = 100})
end
timer.performWithDelay (2000, transition2, 1)

function transition3()
t3 = transition.to (enemyObject, {time = 2000, x = 340, y = 0})
end
timer.performWithDelay (4000, transition3, 1)
end
transitions()

function enemyDeath(event)
enemyObject:removeSelf()
timer.cancel(shootTimer)
transition.cancel (t1)
transition.cancel (t2)
transition.cancel (t3)
end
enemyObject:addEventListener (“tap”, enemyDeath)
end

timer.performWithDelay(1000, spawnEnemy, 3)[/lua] [import]uid: 102017 topic_id: 18161 reply_id: 69795[/import]

This is because all your enemies have the same names - take a look at that and it will help.

Peach :slight_smile: [import]uid: 52491 topic_id: 18161 reply_id: 69846[/import]

Ok, I am starting to feel a little thick headed.

I believe using a table to name the enemies is what you are getting at. Doing that for one enemy works, as before, but doing it for multiple enemies messes up the transitions. This seems to be because by the time all of the enemies are spawned i = 3.

Is there a way to handle the transitions differently than what I have below so that they all occur for each spawned enemy? Or is there perhaps another solution all together that I haven’t been able to deduce?

Thanks again for all of the help.

[lua]local playerObject = display.newCircle (160, 450, 25)

local enemyObject = {}
i = 0
function spawnEnemy()
i = i + 1

enemyObject[i] = display.newCircle (-50, 0, 25)

function enemyShoot()
local enemyBullet = display.newCircle (enemyObject[i].x, enemyObject[i].y, 3)
transition.to (enemyBullet, {time = 2000, x = playerObject.x, y = 500})
end
shootTimer = timer.performWithDelay (1000, enemyShoot, 6)

function transitions()

t1 = transition.to (enemyObject[i], {time = 2000, x = 100, y = 200})

function transition2()
t2 = transition.to (enemyObject[i], {time = 2000, x = 200, y = 100})
end
timer.performWithDelay (2000, transition2, 1)

function transition3()
t3 = transition.to (enemyObject[i], {time = 2000, x = 340, y = 0})
end
timer.performWithDelay (4000, transition3, 1)
end
transitions()
print (i)
function enemyDeath(event)
enemyObject[i]:removeSelf()
timer.cancel(shootTimer)
transition.cancel (t1)
transition.cancel (t2)
transition.cancel (t3)
end
enemyObject[i]:addEventListener (“tap”, enemyDeath)
end

timer.performWithDelay(1000, spawnEnemy, 3)[/lua] [import]uid: 102017 topic_id: 18161 reply_id: 69934[/import]

Keep in mind this isn’t perfect code by any means - but it should move the objects as intended;

[lua]local playerObject = display.newCircle (160, 450, 25)

local enemyObject = {}
i = 0

function spawnEnemy()
i = i + 1

enemyObject[i] = display.newCircle (-50, 0, 25)

function enemyShoot()
local enemyBullet = display.newCircle (enemyObject[i].x, enemyObject[i].y, 3)
transition.to (enemyBullet, {time = 2000, x = playerObject.x, y = 500})
end
shootTimer = timer.performWithDelay (1000, enemyShoot, 6)

function transitions(params)

function transition3()
t3 = transition.to (params, {delay = 4000, time = 2000, x = 340, y = 0})
end
transition3()

function transition2()
t2 = transition.to (params, {delay = 2000, time = 2000, x = 200, y = 100})
end
transition2()

t1 = transition.to (params, {time = 2000, x = 100, y = 200})

end
transitions(enemyObject[i])
print (i)
function enemyDeath(event)
event.target:removeSelf()
timer.cancel(shootTimer)
end
enemyObject[i]:addEventListener (“tap”, enemyDeath)
end

timer.performWithDelay(1000, spawnEnemy, 3)[/lua]

Peach :slight_smile:

PS - Don’t feel thick headed. It’s hard learning this stuff for the first time. [import]uid: 52491 topic_id: 18161 reply_id: 69993[/import]

Hi Peach and Community,

I’ve made good use of your post #10 so got my enemy shooting and transitioning. My problem now is that I would make a loop of my transitions function, which is the following:

[lua]-- My enemy is initially located at x = display.contentWidth/2, y=55

– I make it going by using 3 different stages
function transitions(params)

– stage #1, it goes from the center to the right of the display

function transition1()
t1 = transition.to (params, {delay = 2000, time = 2000, x = display.contentWidth / 1.15, y = 55})
end
transition1()

– stage #2, it goes from the right to the left of the display

function transition2()
t2 = transition.to (params, {delay = 6000, time = 4000, x = display.contentWidth / 8, y = 55})
end
transition2()

– stage #3, it goes from the left to the center of the display

function transition3()
t3 = transition.to (params, {delay = 12000, time = 2000, x = display.contentWidth/2, y = 55})
end
transition3()

end
– calling the function it works without errors…

movemTimer = timer.performWithDelay(1000, transitions(enemy), -1)

– … but it only executes once. Where am I doing wrong?[/lua]
Thanks in advance for your help. Cheers. Giuseppe [import]uid: 67641 topic_id: 18161 reply_id: 70217[/import]

ok, got it myself

the loop is to be inside the function transitions(params)

[lua]function transitions(params)

for tt = 0, 8 do

function transition1()
t1 = transition.to (params, {delay = 2000 + (tt * 14000), time = 2000, x = display.contentWidth / 1.15, y = 55})
end
transition1()

function transition2()
t2 = transition.to (params, {delay = 6000 + (tt * 14000), time = 4000, x = display.contentWidth / 8, y = 55})
end
transition2()

function transition3()
t3 = transition.to (params, {delay = 12000 + (tt * 14000), time = 2000, x = display.contentWidth/2, y = 55})
end
transition3()

function transition4()
t4 = transition.to (params, {delay = 14000 + (tt * 14000), time = 2000, x = display.contentWidth/2, y = 55})
end
transition4()

tt = tt + 1

end

end

movemTimer = transitions(enemy)[/lua]
Now I have the effect I wanted. [import]uid: 67641 topic_id: 18161 reply_id: 70237[/import]

Nicely done :slight_smile:

Peach [import]uid: 52491 topic_id: 18161 reply_id: 70267[/import]