How to Remove Touch Correctly in composer

Hi,

im using composer and touch listener.

but im not sure is the remove touch work,

because when i re enter the scene, it seems it has 2 listener, the action execute twice.

when i reenter again it executed 3 times, and so on.

here’s my listener code

page01.touch = onpage01touch

page01:addEventListener( “touch”, page01 )

page02.touch = onpage02touch

and here’s my removal code

page01:removeEventListener( “touch”, onpage01touch)

 page02:removeEventListener( “touch”, onpage02touch )

thanks

What does your onpage01touch() function look like?

You should not need to remove the event listeners on display objects.  When they are off screen you can’t interact with them.  In the ideal setting you would create the object in the scene:create() function and add the event listener there.  Then as long as the scene’s in memory, you can leave it and come back to it without having to re-create the objects and their touch handlers would be there ready to work.  If the scene gets destroyed, the objects are removed and their handlers with them.  Then the next time you visit the scene, it won’t exist, so it gets re-created with its touch handlers.

The event listeners you have to create/destroy each time entering and leaving scenes are those that start with “Runtime”.  They are not tied to a display object but to the Global runtime.  These you have to start in scene:show() during the “did” phase and stop/remove in the scene:hide() “will” phase.

Rob

Hi Rob,

problem is solved. Seems what cause this error is scroll view. :slight_smile:

i include them in screen view. 

Thanks for the update about listener should remove in show and hide phase. will noted that

What does your onpage01touch() function look like?

You should not need to remove the event listeners on display objects.  When they are off screen you can’t interact with them.  In the ideal setting you would create the object in the scene:create() function and add the event listener there.  Then as long as the scene’s in memory, you can leave it and come back to it without having to re-create the objects and their touch handlers would be there ready to work.  If the scene gets destroyed, the objects are removed and their handlers with them.  Then the next time you visit the scene, it won’t exist, so it gets re-created with its touch handlers.

The event listeners you have to create/destroy each time entering and leaving scenes are those that start with “Runtime”.  They are not tied to a display object but to the Global runtime.  These you have to start in scene:show() during the “did” phase and stop/remove in the scene:hide() “will” phase.

Rob

Hi Rob,

problem is solved. Seems what cause this error is scroll view. :slight_smile:

i include them in screen view. 

Thanks for the update about listener should remove in show and hide phase. will noted that