First, you DO NOT need to remove touch listeners that are part of objects created in the scene ***AS LONG AS** those objects are added to the scene’s “view” or “group”. In @informaticavelasco’s case, loadgame and gogame are never inserted into “group” i.e.
group:insert(loadgame)
By not adding that button to group, storyboard is not managing the button and you’re responsible for removing the button (though removing the button will remove it’s event listeners). I would rewrite initial.lua like this:
local storyboard = require "storyboard"
local scene = storyboard.newScene()
function scene:createScene( event )
local group = self.view
local function gomenu (event)
if event.phase == "ended" then -- you will get both a "began" and "ended", you only want to do this once.
storyboard.gotoScene( "menu", "slideLeft", 800 )
end
return true -- very important to have this line \*here\*
end
loadgame = display.newImage("imagegomenu.png")
loadgame.x = 220
loadgame.y = 100
loadgame:addEventListener( "touch", gomenu )
group:insert(loadgame)
end
function scene:enterScene( event )
local group = self.view
end
scene:addEventListener( "createScene", scene )
scene:addEventListener( "enterScene", scene )
scene:addEventListener( "exitScene", scene )
scene:addEventListener( "destroyScene", scene )
return scene
When you do a storyboard.removeScene(“initial”) in some other scene, everything that is part of “group” will be purged including their event listeners. There is no need to worry about it yourself. Also you really shouldn’t try to remove a scene that you are currently in. Remove it from another scene.
They why are the buttons not behaving? There are several other things that you need to take care of (and I did above). First, since the button was not part of the scene group, it persisted to the next scene, so it was still there. By putting it in group, the first one will disappear when you gotoScene() to the second scene.
Next touch events generate at least two events per tap. A “began” phase and an “ended” phase. Since you are not testing for this, your storyboard.gotoScene() code will execute twice.
Also you are not returning “true” at the end to let the event system know you’ve handled that touch, so the touch will go on to anything underneath the button. Look over my code and see how it differs.
[import]uid: 199310 topic_id: 36396 reply_id: 144601[/import]