[Resolved] Unable to Remove Runtime event listener

Hey Corona-ites,

I’m beating my head against the wall trying to figure this out. I have a simple test case where I’m using director to switch to my play level. On that level, I have an update function that prints, and then a Runtime event listener for that update function. Everything works great until I try to switch back to my menu. I’m using a button with a scene changing/cleaning function. In that function, I am removing the runtime listener. The problem is that the listener isn’t being removed, and when I switch back to the menu, the printing is still happening. Any ideas? I’ll post some code below without all the director business.

Thanks!
–Kev

[code]
–Hide status bar
display.setStatusBar(display.HiddenStatusBar);

local function changeSceneClean()
print(“Cleaning Scene”)
Runtime:removeEventListener(“enterFrame”, update)
cancelAllTransitions()
cancelAllTimers()
director:changeScene (“menu”, “overFromTop”)
end

local replayButton = display.newRect(0, 0, 50, 50)
replayButton.x = _W*0.085;
replayButton.y = _H*0.9;
replayButton.alpha = 0.5;
localGroup:insert(replayButton)
– Replay Button function
local function pressButton (event)
if event.phase == “began” then
replayButton.alpha = 1.0
transitionStash.newTransition=transition.to(event.target, {time=50, xScale=1.25, yScale=1.25})
end
if event.phase == “ended” then
replayButton.alpha = 0.5
changeSceneClean()
end
end
replayButton:addEventListener (“touch”, pressButton)
local function update(event)
print(“Still updating”)
end
Runtime:addEventListener(“enterFrame”, update)
[/code] [import]uid: 70550 topic_id: 27429 reply_id: 327429[/import]

One issue i have had in the past is calling a runtime remove before i call the runtime event. Even though the event is already created when i call the remove, if i try to call the remove above where the event was created then it wont get removed. This was an issue i had a while back so i do not know if it has been resolved or not. One way you could test this is to move your whole update function with the runtime event to the top of the file and see if it gets removed then. Another suggestion is to make use of directors clean() function. It is a function that will be called after the current scene has been unloaded. That is where i would put my remove run time events.

[lua]function new()

local scene = display.newGroup()

–Here is where you would put the clean function
function clean ()

–Put what ever you want to clean up here
Runtime:removeEventListener (“enterFrame”, update)

end
return scene
end[/lua]

Director will call that clean function so its an easy way to clean up anything that you do not want to go to the next scene especially globals and runtime events [import]uid: 126161 topic_id: 27429 reply_id: 111429[/import]

Holy smokes! That was it!! The listener had to exist in the code above the removal snippet.

Thanks 3 Dreams!!

:slight_smile:
–Kev [import]uid: 70550 topic_id: 27429 reply_id: 111432[/import]

No Problem. That is just from experience that’s all. I had the same problem and took me quite some time to figure that out lol. I figured since it was runtime then it was global and i could call it anywhere and remove it anywhere but obviously that’s not the case. [import]uid: 126161 topic_id: 27429 reply_id: 111433[/import]

Thank u very much.
[import]uid: 131755 topic_id: 27429 reply_id: 128872[/import]

Thank u very much.
[import]uid: 131755 topic_id: 27429 reply_id: 128872[/import]