add/remove listeners : contradictions between tutorials and sample files

After reading all composer tutorial they say :

In scene:create – Example: add display objects to “sceneGroup”, add touch listeners, etc.

But, in the Samples/Interface/Composer, we have the following :

In scene:create        image.touch = onSceneTouch

In scene:show         image:addEventListener( “touch”, image )

In scene:hide           image:removeEventListener( “touch”, image )

So, what is the best practice ?

Thx

You should add touch listeners in scene:show. That way, people can’t touch/tap your objects before the scene is fully “in”.

  • Caleb

Thx for the answer. But doing so kills my listener : 

I do EXACTLY the same code on scene1.lua and scene2.lua :

-- outside of the composer listener functions local background local function backgroundTouch( self, event) -- standard touch fonction with composer.gotoScene( "scene2") -- of course, in scene2 I do composer.gotoScene( "scene1" ) end -- In scene:create background = display.newRect( 0, 0, width, height ) background.touch = backgroundTouch -- In scene:show (did) background:addEventListener( "touch", background ) -- In scene:hide (will) background:removeEventListener( "touch", background )

My problem : I start on scene1, go to scene2, but impossible to go back to scene1 (as if the background listener was removed)

You should add touch listeners in scene:show. That way, people can’t touch/tap your objects before the scene is fully “in”.

  • Caleb

Thx for the answer. But doing so kills my listener : 

I do EXACTLY the same code on scene1.lua and scene2.lua :

-- outside of the composer listener functions local background local function backgroundTouch( self, event) -- standard touch fonction with composer.gotoScene( "scene2") -- of course, in scene2 I do composer.gotoScene( "scene1" ) end -- In scene:create background = display.newRect( 0, 0, width, height ) background.touch = backgroundTouch -- In scene:show (did) background:addEventListener( "touch", background ) -- In scene:hide (will) background:removeEventListener( "touch", background )

My problem : I start on scene1, go to scene2, but impossible to go back to scene1 (as if the background listener was removed)