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 >= 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] </…>
[import]uid: 3826 topic_id: 10591 reply_id: 38515[/import] </…>