removeEventListener doesn't work!

Hi. I made a simple test wich proves that removeEventListernet does not work.

It simply shows 2 buttons and 1 variable (text). One button for adding +1 to the variable and update the screen (show the new value of the variable), and one button  for (try) stop the other button to work. But it can’t! Why?


variable=1

function the_test()
    local button_plus
    local button_stop

    local function show_everything()
        display.newImage(“background.png”,0,0)
        button_plus=display.newImage(“button_plus.png”, 45, 375 )

        button_stop=display.newImage(“button_stop.png”,147,270)
        
        local myText = display.newText( "variable = " … variable, 0, 0, “Arial”, 40 )
    end

    show_everything()
    
    local function variable_update()
        variable=variable+1
        show_everything()
        return true
    end

    
    local function stop_variableUpdates()
        button_plus:removeEventListener( “tap”, variable_update )                             <----- does not work!!!
        return true
    end

    button_plus:addEventListener( “tap”, variable_update )
    button_stop:addEventListener( “tap”, stop_variableUpdates )

end

—start here:
the_test()
 


I press the button_stop, so it calls the function stop_variableUpdates() wich contains:
        button_plus:removeEventListener( “tap”, variable_update )

So the button_plus must stop working…

But if I press it, the variable still continues increase…

Why this is not working? How can I fix that?

Please help me. Thanks!

This is probably because in your show_everything function you keep creating NEW display objects each time it is called. So you are in fact covering the old one with a new image and text in the exact same spot. What is it that you are trying to achieve? To update the text just call myText.text

I’m have a similar problem, but on a much simpler scale:

local function click(button) print("click") button:removeEventListener("tap", function() click(button) end) end local button = display.newRect(100, 100, 100, 100) button:addEventListener("tap", function() click(button) end)

In your case Matthew you’re using anonymous functions and these will both refer to different instances in memory. There’s an old topic in the forum on the subject, I believe it was named something like “Advanced OOP removeEventListener question…”

This is probably because in your show_everything function you keep creating NEW display objects each time it is called. So you are in fact covering the old one with a new image and text in the exact same spot. What is it that you are trying to achieve? To update the text just call myText.text

I’m have a similar problem, but on a much simpler scale:

local function click(button) print("click") button:removeEventListener("tap", function() click(button) end) end local button = display.newRect(100, 100, 100, 100) button:addEventListener("tap", function() click(button) end)

In your case Matthew you’re using anonymous functions and these will both refer to different instances in memory. There’s an old topic in the forum on the subject, I believe it was named something like “Advanced OOP removeEventListener question…”