Runtime Listener still running...

I am calling Runtime:addEventListener(“enterFrame”,map.updateView) and then remove the listener with scene change: Runtime:removeEventListener(“enterFrame”,map.updateView)

BUT when not moving to a new level and instead trying to play the same level again I have the problem the listener seems to be still running and trying to update, but I then get a Runtime Error because the camera object is NIL

I am working with an empty scene between scene changes, so I thought this will “delete” my old level scene in all cases, even when calling the same level again… BUT somehow there seems to be something left running here.

I have looked into all the composer details and I think maybe this has to do with the runtime listener in dusk?

Anyone have heard about this kind of problem maybe?

There are two possibilities I can think  of.

  1. The value of map.updateView changed between the time you added the listener and the time you’re removing it. 

  2. When you are removing it, map is unknown. This could be a scoping issue.  Consider:

    local map = {} local function endScene()        Runtime:removeEventListener(“enterFrame”,map.updateView)  --<------ map.updateView is currently nil end function map.updateView()     – do stuff end local function startScene()      Runtime:addEventListener(“enterFrame”,map.updateView)  --<------ map.updateView has a value now end

To remove a listener both the type, i.e. “enterFrame” and the address of the function must match what was used when the event handler was created. In this case you’re saying:  Look up the “enterFrame” nil function to remove. It can’t find it in it’s table since it’s not there. What’s in the table is “enterFrame”, 0x98AB4201 (made up memory address for example). And it can’t remove it.

Rob

Thank you Rob for your fast help! I appreciate it!

I will look into this asap.

Assuming you want to call the map class externally then this is a more robust implementation

local map = {} local function map:enterFrame() -- do stuff end function map:endScene() Runtime:removeEventListener("enterFrame", self) end function map:startScene() Runtime:addEventListener("enterFrame", self) end

map.updateView is an object method and as such must be called with a colon; that means you’ll need some sort of closure:

local enterFrameListener = function(event) map:updateView() end -- scene create Runtime:addEventListener("enterFrame", enterFrameListener) -- scene remove Runtime:removeEventListener("enterFrame", enterFrameListener)

Most of the time, map:updateView() is put inside the game-wide enter frame loop. Try that and see if it fixes it.

  • Caleb

There are two possibilities I can think  of.

  1. The value of map.updateView changed between the time you added the listener and the time you’re removing it. 

  2. When you are removing it, map is unknown. This could be a scoping issue.  Consider:

    local map = {} local function endScene()        Runtime:removeEventListener(“enterFrame”,map.updateView)  --<------ map.updateView is currently nil end function map.updateView()     – do stuff end local function startScene()      Runtime:addEventListener(“enterFrame”,map.updateView)  --<------ map.updateView has a value now end

To remove a listener both the type, i.e. “enterFrame” and the address of the function must match what was used when the event handler was created. In this case you’re saying:  Look up the “enterFrame” nil function to remove. It can’t find it in it’s table since it’s not there. What’s in the table is “enterFrame”, 0x98AB4201 (made up memory address for example). And it can’t remove it.

Rob

Thank you Rob for your fast help! I appreciate it!

I will look into this asap.

Assuming you want to call the map class externally then this is a more robust implementation

local map = {} local function map:enterFrame() -- do stuff end function map:endScene() Runtime:removeEventListener("enterFrame", self) end function map:startScene() Runtime:addEventListener("enterFrame", self) end

map.updateView is an object method and as such must be called with a colon; that means you’ll need some sort of closure:

local enterFrameListener = function(event) map:updateView() end -- scene create Runtime:addEventListener("enterFrame", enterFrameListener) -- scene remove Runtime:removeEventListener("enterFrame", enterFrameListener)

Most of the time, map:updateView() is put inside the game-wide enter frame loop. Try that and see if it fixes it.

  • Caleb