Runtime event listener not removing

Hello everyone I’ve been having problem with removing Runtime event listener. It is to my understanding that in order to remove a Runtime event listener you must remove it within the function it’s calling. So that is exactly what I did. But the problem is, it’s not removing the listener.

Here’s my code

[lua]

local storyboard = require"storyboard"
local widget = require"widget"
local scene = storyboard.newScene()
local screenGroup
local button
local _W = display.contentWidth
local _H = display.contentHeight

globalVar = 0

local function nextScene(event)
    storyboard.gotoScene(“scene1”, “fade”, 100)
    globalVar = 1
end

local function printFrameContent()
    if globalVar == 0 then
        print(“Scene 2”)
    elseif globalVar == 1 then
        print(“Remove listener”)
        Runtime:addEventListener(“enterFrame”, printFrameContent) – Here is the remove listener
    end
end

function scene:createScene( event )
    screenGroup = self.view;
    button = widget.newButton{
        width = 120,
        height = 120,
        label = “Scene 1”,
        onRelease = nextScene
    }
    button.x = _W /2; button.y = _H / 2;
    screenGroup:insert(button)
end

function scene:enterScene( event )
    local prev = storyboard.getPrevious()
    if(prev ~= nil) then
        storyboard.removeScene(prev)
    end
    Runtime:addEventListener(“enterFrame”, printFrameContent) – Here is the listener declaration.
end

function scene:exitScene( event )
    scene:removeEventListener( “createScene”, scene )
    scene:removeEventListener( “enterScene”, scene )
    scene:removeEventListener( “exitScene”, scene )
    scene:removeEventListener( “destroyScene”, scene )
end

function scene:destroyScene( event )
    
end

scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )

return scene

[/lua]

As you can see I place a print just to see if the listener has been removed. But it is still printing even after decliaring the remove listener.

Just a note before I get on to answering the question:

I believe there is no need to add or remove the four default scene functions from a storyboard.

If you notice, they all have names like scene:createScene, which means they are tied to the scene object you declared at the top of the page. Since the scene object is local it is removed when the class is unloaded, which happens just after scene:destroy automatically (due to the storyboard code). Since the object the functions are attached to is gone, so are the functions themselves.

Now onto your actual question, which was about removing the runtime listener.

[lua]

    Runtime:addEventListener( “enterFrame”, printFrameContent )

[/lua]

should be

[lua]

    Runtime:removeEventListener( “enterFrame”, printFrameContent )

[/lua]

Since Runtime is a global value, this can be done anywhere, but removing it when you no longer need it in its own method is a good idea. Alternativly, if you want it to be possible to trigger it multiple times, remove it in destoryScene or exit scene as you prefer.

Well I felt stupid. I didn’t saw it was add and not remove hahah. I did not know that you can use storyboard to remove Runtime listeners. Thanks man. At least I learn something new because of my stupidity.

Just a note before I get on to answering the question:

I believe there is no need to add or remove the four default scene functions from a storyboard.

If you notice, they all have names like scene:createScene, which means they are tied to the scene object you declared at the top of the page. Since the scene object is local it is removed when the class is unloaded, which happens just after scene:destroy automatically (due to the storyboard code). Since the object the functions are attached to is gone, so are the functions themselves.

Now onto your actual question, which was about removing the runtime listener.

[lua]

    Runtime:addEventListener( “enterFrame”, printFrameContent )

[/lua]

should be

[lua]

    Runtime:removeEventListener( “enterFrame”, printFrameContent )

[/lua]

Since Runtime is a global value, this can be done anywhere, but removing it when you no longer need it in its own method is a good idea. Alternativly, if you want it to be possible to trigger it multiple times, remove it in destoryScene or exit scene as you prefer.

Well I felt stupid. I didn’t saw it was add and not remove hahah. I did not know that you can use storyboard to remove Runtime listeners. Thanks man. At least I learn something new because of my stupidity.