problem with addEventListener and Storyboard!

Hi!
The problem is this:

I have three files: initial.lua, menu.lua and game.lua.
I have addEventListener in initial.lua, everything runs ok. But when I’m in game.lua, if I squeeze in the position where the returns to menú.lua addEventListener. I tried deleting the addEventListener but got no result at all. How I can delete it and get my code to work correctly?.
Here I show my simplified code, I know where the error but does not solve it. The game.lua not show it because I know that there is no error and it would be more confusing.
Thank you very much in advance for your help.

INITIAL.LUA
[lua]

module(…, package.seeall)
local storyboard = require “storyboard”
local scene = storyboard.newScene()

function scene:createScene( event )
local group = self.view

local function gomenu ()
storyboard.gotoScene( “menu”, “slideLeft”, 800 )
storyboard.removeScene( “initial” )
end
loadgame = display.newImage(“imagegomenu.png”)
loadgame.x = 220
loadgame.y = 100
loadgame:addEventListener( “touch”, gomenu )

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

[/lua]
MENU.LUA
[lua]
module(…, package.seeall)
local storyboard = require “storyboard”
local scene = storyboard.newScene()

function scene:createScene( event )
local group = self.view

local function goGame ()
storyboard.gotoScene( “game”, “slideLeft”, 800 )
storyboard.removeScene( “menu” )
end
gogame = display.newImage(“imagegogame.png”)
gogame.x = 220
gogame.y = 300
gogame:addEventListener( “touch”, goGame )

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

[/lua]
[import]uid: 192093 topic_id: 36396 reply_id: 336396[/import]

If you want to remove event listeners, I usually do it in the exitScene() function in storyboard e.g.

[code]
function scene:exitScene( event )

print( “1: exitScene event” )
– remove touch listener for image
image:removeEventListener( “touch”, image )

end
[/code] [import]uid: 208811 topic_id: 36396 reply_id: 144521[/import]

I did what I indicastes but I get this error in the console by clicking on the button.
[lua]
1: exitScene event
Runtime error
ERROR: nil key supplied for property lookup.
stack traceback:
[C]: ?
[C]: ?
?: in function ‘removeEventListener’
?: in function ‘removeEventListener’

[/lua]

I did this:
[lua]

loadgame:removeEventListener( “touch”, loadgameFunction )
[/lua]
in exit scene. [import]uid: 192093 topic_id: 36396 reply_id: 144523[/import]

Ok my implementation is slightly different. Here is an example for what I use for the user to press a button to switch to a different scene.

function scene:createScene( event )  
  
 local function onSceneTouch( self, event )  
 if event.phase == "began" then  
  
 storyboard.gotoScene( "info", "slideLeft", 1000 )  
  
 return true  
 end  
 end  
  
 infoBtn = display.newImage("images/info.png", 440, 10 )  
 infoBtn.touch = onSceneTouch  
 infoBtn:addEventListener("touch", infoBtn)  
end  
function scene:exitScene()  
  
 print( "2: exitScene event" )  
  
 infoBtn:removeEventListener("touch", infoBtn)  
  
  
end  
  

I hope this helps you [import]uid: 208811 topic_id: 36396 reply_id: 144524[/import]

ok, and everything works correctly, however although it works and does not block the application, it shows me an error on the console. If I miss it … Will it happen something? The app works properly despite showing that error.

[lua]
Runtime error
assertion failed!
stack traceback:
[C]: ?
[C]: in function ‘assert’
?: in function ‘getOrCreateTable’
?: in function ‘addEventListener’
?: in function ‘addEventListener’

[/lua]
[import]uid: 192093 topic_id: 36396 reply_id: 144531[/import]

I’m not sure what this will do to your app, I do not get this error. Does it tell you in what line of the code that the error is being caused. You could probably post this error as another question in the forum [import]uid: 208811 topic_id: 36396 reply_id: 144539[/import]

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]

If you want to remove event listeners, I usually do it in the exitScene() function in storyboard e.g.

[code]
function scene:exitScene( event )

print( “1: exitScene event” )
– remove touch listener for image
image:removeEventListener( “touch”, image )

end
[/code] [import]uid: 208811 topic_id: 36396 reply_id: 144521[/import]

I did what I indicastes but I get this error in the console by clicking on the button.
[lua]
1: exitScene event
Runtime error
ERROR: nil key supplied for property lookup.
stack traceback:
[C]: ?
[C]: ?
?: in function ‘removeEventListener’
?: in function ‘removeEventListener’

[/lua]

I did this:
[lua]

loadgame:removeEventListener( “touch”, loadgameFunction )
[/lua]
in exit scene. [import]uid: 192093 topic_id: 36396 reply_id: 144523[/import]

Ok my implementation is slightly different. Here is an example for what I use for the user to press a button to switch to a different scene.

function scene:createScene( event )  
  
 local function onSceneTouch( self, event )  
 if event.phase == "began" then  
  
 storyboard.gotoScene( "info", "slideLeft", 1000 )  
  
 return true  
 end  
 end  
  
 infoBtn = display.newImage("images/info.png", 440, 10 )  
 infoBtn.touch = onSceneTouch  
 infoBtn:addEventListener("touch", infoBtn)  
end  
function scene:exitScene()  
  
 print( "2: exitScene event" )  
  
 infoBtn:removeEventListener("touch", infoBtn)  
  
  
end  
  

I hope this helps you [import]uid: 208811 topic_id: 36396 reply_id: 144524[/import]

ok, and everything works correctly, however although it works and does not block the application, it shows me an error on the console. If I miss it … Will it happen something? The app works properly despite showing that error.

[lua]
Runtime error
assertion failed!
stack traceback:
[C]: ?
[C]: in function ‘assert’
?: in function ‘getOrCreateTable’
?: in function ‘addEventListener’
?: in function ‘addEventListener’

[/lua]
[import]uid: 192093 topic_id: 36396 reply_id: 144531[/import]

I’m not sure what this will do to your app, I do not get this error. Does it tell you in what line of the code that the error is being caused. You could probably post this error as another question in the forum [import]uid: 208811 topic_id: 36396 reply_id: 144539[/import]

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]

If you want to remove event listeners, I usually do it in the exitScene() function in storyboard e.g.

[code]
function scene:exitScene( event )

print( “1: exitScene event” )
– remove touch listener for image
image:removeEventListener( “touch”, image )

end
[/code] [import]uid: 208811 topic_id: 36396 reply_id: 144521[/import]

I did what I indicastes but I get this error in the console by clicking on the button.
[lua]
1: exitScene event
Runtime error
ERROR: nil key supplied for property lookup.
stack traceback:
[C]: ?
[C]: ?
?: in function ‘removeEventListener’
?: in function ‘removeEventListener’

[/lua]

I did this:
[lua]

loadgame:removeEventListener( “touch”, loadgameFunction )
[/lua]
in exit scene. [import]uid: 192093 topic_id: 36396 reply_id: 144523[/import]

Ok my implementation is slightly different. Here is an example for what I use for the user to press a button to switch to a different scene.

function scene:createScene( event )  
  
 local function onSceneTouch( self, event )  
 if event.phase == "began" then  
  
 storyboard.gotoScene( "info", "slideLeft", 1000 )  
  
 return true  
 end  
 end  
  
 infoBtn = display.newImage("images/info.png", 440, 10 )  
 infoBtn.touch = onSceneTouch  
 infoBtn:addEventListener("touch", infoBtn)  
end  
function scene:exitScene()  
  
 print( "2: exitScene event" )  
  
 infoBtn:removeEventListener("touch", infoBtn)  
  
  
end  
  

I hope this helps you [import]uid: 208811 topic_id: 36396 reply_id: 144524[/import]

ok, and everything works correctly, however although it works and does not block the application, it shows me an error on the console. If I miss it … Will it happen something? The app works properly despite showing that error.

[lua]
Runtime error
assertion failed!
stack traceback:
[C]: ?
[C]: in function ‘assert’
?: in function ‘getOrCreateTable’
?: in function ‘addEventListener’
?: in function ‘addEventListener’

[/lua]
[import]uid: 192093 topic_id: 36396 reply_id: 144531[/import]

I’m not sure what this will do to your app, I do not get this error. Does it tell you in what line of the code that the error is being caused. You could probably post this error as another question in the forum [import]uid: 208811 topic_id: 36396 reply_id: 144539[/import]

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]

If you want to remove event listeners, I usually do it in the exitScene() function in storyboard e.g.

[code]
function scene:exitScene( event )

print( “1: exitScene event” )
– remove touch listener for image
image:removeEventListener( “touch”, image )

end
[/code] [import]uid: 208811 topic_id: 36396 reply_id: 144521[/import]