How to remove objects after transition

Hi!
I can’t figure out how to handle transitions and removement of objects.

I have an enemy helictopter (copter) that transition from right side of the display to the left side of the display. The hero can shoot bullets on the helicopter. After 5 hits the helicopter dies. The thing is, I want the helicopter to remove itself if the hero doesn’t hit the helicopter.

I’ve tried with onComplete = function(self) self.parent:remove(self); self = nil; end}. It works if the hero doesn’t kill the helicopter, but if he does, I get this error message:

.../level1.lua:1202: attempt to call method 'remove' (a nil value) stack traceback: [C]: in function 'remove' .../level1.lua:1202: in function <...><br> (tail call): ?<br> ?: in function <?:930><br> ?: in function <?:214><br>

My code:

<br>function onCollision(self, event)<br> if (self.name == "bullet" and event.other.name == "copter") then<br> <br> copter.hit = copter.hit + 1<br><br> if copter.hit == 5 then<br> local newScore = gameScore + 100<br> setScore( newScore )<br> <br> event.other:removeSelf()<br> self:removeSelf() <br> else<br> self:removeSelf() <br> end<br> end<br>end<br><br>local timeLastCopter = 0<br><br>local gameLoop = function(event)<br><br> stringHeight = math.random(20, 130) -- Random height<br> if event.time - timeLastCopter &gt;= math.random(10000, 12000) then<br><br> local sheet = sprite.newSpriteSheet( "images/enemies/copter.png", 162, 76 )<br><br> thetime = math.random(3000, 7000)<br><br> local spriteSet = sprite.newSpriteSet(sheet, 1, 1)<br> sprite.add( spriteSet, "copter", 1, 4, 100, 0 ) <br><br> copter = sprite.newSprite( spriteSet )<br> copter.x = 520<br> copter.y = stringHeight<br> copter:prepare("copter")<br> copter:play()<br> copter.hit = 0 <br> <br> copter.name = "copter"<br> copterShape = { 73, -12 , 70, 13 , -23, 27 , -81, 3 }<br> physics.addBody(copter, "kinematic", {bounce = 0, shape=copterShape})<br><br> transition.to( copter, { time=thetime, alpha=1, x=-80, y=stringHeight, onComplete = function(self) self.parent:remove(self); self = nil; end} )<br> <br> gameGroup:insert(copter)<br> timeLastCopter = event.time<br> end<br>end<br>

How should I do it?

Best regards,
joelwe [import]uid: 54640 topic_id: 10591 reply_id: 310591[/import] </…>

I assume line 1202 is line 13 in the code above, if that’s the case once the copter is hit 5 times
it’s removed at line 11, then at line 13 remove is called again on an object which no longer exists. [import]uid: 43696 topic_id: 10591 reply_id: 38513[/import]

No line 1202 is line 43 in the code above.

Line 43:

transition.to( copter, { time=thetime, alpha=1, x=-80, y=stringHeight, onComplete = function(self) self.parent:remove(self); self = nil; end} )

It’s supposed to remove the copter when the transition has ended.


Well, I came up with a solution (thx lerg!).

I made a new variable called ‘isAlive’, which is set to true. And I made a new function called ‘removeCopter’ which get called when the transition of the copter is complete. And it checks if the copter is dead, if not, it removes it.

Best regards,
joelwe

[code]

function onCollision(self, event)
if (self.name == “bullet” and event.other.name == “copter”) then

copter.hit = copter.hit + 1

if copter.hit == 5 then
copter.isAlive = false

local newScore = gameScore + 100
setScore( newScore )

event.other:removeSelf()
self:removeSelf()
else
self:removeSelf()
end
end
end

local timeLastCopter = 0

local gameLoop = function(event)

stringHeight = math.random(20, 130) – Random height
if event.time - timeLastCopter >= math.random(10000, 12000) then

local function removeCopter(event)
if copter.isAlive = true then
copter:removeSelf()
copter = nil
end
end

local sheet = sprite.newSpriteSheet( “images/enemies/copter.png”, 162, 76 )

thetime = math.random(3000, 7000)

local spriteSet = sprite.newSpriteSet(sheet, 1, 1)
sprite.add( spriteSet, “copter”, 1, 4, 100, 0 )

copter = sprite.newSprite( spriteSet )
copter.x = 520
copter.y = stringHeight
copter:prepare(“copter”)
copter:play()
copter.hit = 0

copter.isAlve = true

copter.name = “copter”
copterShape = { 73, -12 , 70, 13 , -23, 27 , -81, 3 }
physics.addBody(copter, “kinematic”, {bounce = 0, shape=copterShape})

transition.to( copter, { time=thetime, alpha=1, x=-80, y=stringHeight, onComplete = removeCopter} )

gameGroup:insert(copter)
timeLastCopter = event.time
end
end
[/code] [import]uid: 54640 topic_id: 10591 reply_id: 38514[/import]

@joelwe

…/level1.lua:1202: attempt to call method ‘remove’ (a nil value)
stack traceback:
[C]: in function ‘remove’
…/level1.lua:1202: in function <…>
(tail call): ?
?: in function <?:930>
?: in function <?:214>

it states that there is no such function as remove.

Now when you are using removeSelf(), why are you switching to self.parent:remove(self)?

just use

onCompletion = function() copter:removeSelf() end

and you shall be fine

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 10591 reply_id: 38515[/import] </…>

Reviving an old thread since I couldn’t find a solution to this in the forum.

instead of doing:

transition.to(obj, {time=1000, x=100, onComplete=function()  
 obj:removeSelf()  
end})  

use pcall to prevent a nil error from showing up. checking the obj if it is nil apparently doesn’t work:

transition.to(obj, {time=1000, x=100, onComplete=function() if pcall(function() obj:removeSelf() end) then else obj = nil -- just making sure end end}) [import]uid: 144908 topic_id: 10591 reply_id: 113314[/import]