I’m re-pasting this code using PURE corona for future readers and so you can see the difference.
-- Finalize listener - Called when object is destroyed. local function onFinalize( self ) -- name is not important, this is my convention Runtime:removeEventListener( "enterFrame", self ) end -- If your enterFrame listeners all do the same thing, just -- create a single function and re-use it. local function onEnterFrame( self ) -- name is not important, this is my convention if gamePaused then return true end --do this with 'self' --do that with 'self' end for i = 1, #numEnemies do enemy[i].enterFrame = onEnterFrame enemy[i].finalize = onFinalize Runtime:addEventListener( "enterFrame", enemy[i] ) enemy[i]:addEventListener( "finalize" ) end
The two major differences are:
- ‘Runtime:addEventListener()’ is simply ‘listen()’ in SSK which is much easier to type.
- ‘Runtime:removeEventListener()’ is replaced by ‘ignoreList()’ in the prior code.
- The direct SSK equivalent is actually ‘ignore()’ (again easier to type).
Why Use ignoreList()?
If you write your code the way I suggested:
local function onEnterFrame( self ) end ... then later obj.enterFrame = onEnterFrame .. and finally listen( "enterFrame", obj )
You can use ‘ignoreList()’ instead of ‘ignore()’.
It has a couple of nice features:
- It is safer and will not attempt to ignore an event that was never listened for or has otherwise already had the listener removed.
- If you try to call Runtime:removeEventListener() twice on a object, the second call will crash. ‘ignoreList()’ is smart enough to avoid this.
- You can list all of global listeners you want to ignore in one line.
Example of #2. Let’s assume you are also listenering for ‘mouse’ and ‘accelerometer’, you could then do this:
ignoreList( { "enterFrame", "mouse", "accelerometer" }, obj )