onComplete removal problems

I’m having troubles removing transition objects in my game. During a power up, the player gets to shoot 5 bullets. The problem is, only the initial bullet gets removed, the other 4 just sit there. My terminal gives the error on the transition.to “lua:334: attempt to index global ‘bullet’ (a nil value)”. But I obviously can’t nil it out. What should I do to correct this?

[code]
local powerup1 = function()

powerup1 = true

local bullets = 5

local firebutton = display.newImage(“trigger.png”)
firebutton.x = 420
firebutton.y = 255
firebutton.isVisible = true
localGroup:insert(firebutton)

local fire = function(event)
if event.phase == “ended” then
bullet = display.newImageRect(“bullet.png”, 5, 30)
bullet.x = player.x + 50
bullet.y = player.y
bullet.myName = “bullet”
transition.to (bullet, {time = 500, x=400, onComplete =function()bullet:removeSelf();bullet=nil;end })
physics.addBody (bullet, {density = 0.1 })
localGroup:insert(bullet)
bullets = bullets - 1
print(“Shot bullet”)

local destroyPowerup1 = function()
if bullets == 0 then
firebutton.isVisible = false

powerup1 = false

end
end
destroyPowerup1()
end
end
firebutton:addEventListener(“touch”, fire)
end [import]uid: 114389 topic_id: 31811 reply_id: 331811[/import]

@Silky,

I think I see the problem, but I’m not 100% sure. Try changing your code: transition.to (bullet, {time = 500, x=400, onComplete =function()bullet:removeSelf();bullet=nil;end })

to this: local closure = function() bullet:removeSelf(); bullet=nil; end transition.to (bullet, {time = 500, x=400, onComplete = closure })

Maybe it won’t make a difference, but I have a feeling there is something wonky about creating the closure inline like that?

This is just a shot in the dark.

If that doesn’t work just let me know and I’ll investigate further.

Cheers,
Ed

[import]uid: 110228 topic_id: 31811 reply_id: 127012[/import]

I would also suggest changing this: bullet = display.newImageRect("bullet.png", 5, 30)

to this: local bullet = display.newImageRect("bullet.png", 5, 30) [import]uid: 110228 topic_id: 31811 reply_id: 127013[/import]

That worked! Thanks a lot emaurina :slight_smile: [import]uid: 114389 topic_id: 31811 reply_id: 127043[/import]

@Silky,

I think I see the problem, but I’m not 100% sure. Try changing your code: transition.to (bullet, {time = 500, x=400, onComplete =function()bullet:removeSelf();bullet=nil;end })

to this: local closure = function() bullet:removeSelf(); bullet=nil; end transition.to (bullet, {time = 500, x=400, onComplete = closure })

Maybe it won’t make a difference, but I have a feeling there is something wonky about creating the closure inline like that?

This is just a shot in the dark.

If that doesn’t work just let me know and I’ll investigate further.

Cheers,
Ed

[import]uid: 110228 topic_id: 31811 reply_id: 127012[/import]

I would also suggest changing this: bullet = display.newImageRect("bullet.png", 5, 30)

to this: local bullet = display.newImageRect("bullet.png", 5, 30) [import]uid: 110228 topic_id: 31811 reply_id: 127013[/import]

That worked! Thanks a lot emaurina :slight_smile: [import]uid: 114389 topic_id: 31811 reply_id: 127043[/import]