about stopping the enterFrame

[lua] function dartRotation (self,event)

self.rotation = self.rotation - self.roSpeed

end

enemyDartGroup = display.newGroup ()
–enemyGroup.anchorChildren = true
screenGroup:insert (enemyDartGroup)

function moveEnemy ()
     for a = enemyDartGroup.numChildren, 1, -1 do

          if ( enemyDartGroup[a].x > -120 ) then
          enemyDartGroup[a].x = enemyDartGroup[a].x - 2
     else
          enemyDartGroup[a]:removeEventListener (“enterFrame”, dartRotation)
          enemyDartGroup:remove (enemyDartGroup[a])
          enemyDartGroup[a] = nil
     end
     end
end

function spawnDart ()

dart = display.newImageRect (“media/images/dart.png”, 40, 40)
dart.x = w * 0.5; dart.y = h * 0.5
physics.addBody (dart, “static”, physicsData:get(“dart”))
dart.roSpeed = 5
screenGroup:insert (dart)
enemyDartGroup:insert (dart)
screenGroup:insert (enemyDartGroup)

dart.enterFrame = dartRotation
Runtime:addEventListener (“enterFrame”, dart)

if dartMoving == false then

     print (“dart is now moving”)
     dartMoving = true

     moveDartTmr = timer.performWithDelay ( 15, moveEnemy, -1 )

end

end[/lua]

I have something moving out from the screen, like flappy bird’s pipe 

but, the thing was on the dartRotation function, and every time the thing is going to remove, 

Attempt to perform arithmetic on field ‘rotation’ (a nil value) which is this lane

[lua] function dartRotation (self,event)

self.rotation = self.rotation - self.roSpeed

end

[lua]

After that, I added enemyDartGroup[a]:removeEventListener (“enterFrame”, dartRotation) to stop the rotation.

it doesn’t help also.

What i want to do is the dart will rotation after it spawns, and move the dart.x - 2.

When the dart is < -120 then remove itself and stop rotating.

but now when the dart is going to remove itself has a problem in dartRotation function

what am i do wrong here?

Try this:

function dartRotation (self,event) if ( self.removeSelf == nil ) then Runtime:removeEventListener( "enterFrame", self ) return end self.rotation = self.rotation - self.roSpeed end

Ok… I think it works… but can i ask how this work and different between mine?

And why do we need to return after Runtime:removeEventListener( “enterFrame”, self )

If you don’t return, you’ll execute the next line of code and crash because the object no longer exists.

The code I wrote is detecting that the object was destroyed and all that is left is the table stub Corona leaves behind till Lua garbage collects it.  

After destruction if you try to treat a table stub as a display object you crash the engine.  This is what you were encountering.

Thanks!!

Try this:

function dartRotation (self,event) if ( self.removeSelf == nil ) then Runtime:removeEventListener( "enterFrame", self ) return end self.rotation = self.rotation - self.roSpeed end

Ok… I think it works… but can i ask how this work and different between mine?

And why do we need to return after Runtime:removeEventListener( “enterFrame”, self )

If you don’t return, you’ll execute the next line of code and crash because the object no longer exists.

The code I wrote is detecting that the object was destroyed and all that is left is the table stub Corona leaves behind till Lua garbage collects it.  

After destruction if you try to treat a table stub as a display object you crash the engine.  This is what you were encountering.

Thanks!!