Removing EventListeners while going from one scene to another

hi,i am running into a problem again and again when i go from one scene to another.

In my   game.lua file.i have a created a button to control the player and a collision function.i am unable to remove the touch event of the button when i go to gameover.lua.

A small portion of my code looks like this:-

local composer=require("composer") local scene=composer.newScene() local centerX=display.contentWidth\*0.5; local centerY=display.contentHeight\*0.5; local physics=require("physics"); physics.start(); physics.setGravity(0,10); ------------ local right,local player,local ball; ------------ function scene:create(event) player=display.newImageRect("player.png",35,35); player.x=centerX-145; player.y=centerY-70; physics.addBody(player, "dynamic"); sceneGroup:insert(player); ball=display.newImageRect("ball.png",15,15); ball.x=100; ball.y=100; physics.addBody(ball, "dynamic"); sceneGroup:insert(ball); up=display.newImageRect("up.png",80,80); up.x=centerX+250; up.y=centerY+15; up.alpha=0.5; sceneGroup:insert(right); end local function upkey(event) if(event.phase=="began")then a1=transition.to(player,{time=100,y=player.y-150}); end end function scene:show(event) local sceneGroup=self.view local phase = event.phase if(phase=="will") then elseif(phase=="did")then up:addEventListener("touch",upkey); end end -- there is also a function for collision function scene:hide(event) right:removeEventListener("touch",rightkey); end function scene:destroy(event) transition.cancel(a1) end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ------------------------------------------ return scene

i have another button(restart) in restart.lua at the same x and y co ordinate of upbutton so

when i go to gameover.lua on collision of player with the ball

the button restart gets triggerered on its own.

can anyone tell me  how do i remove this touch event when i go from one scene to another.


also i have a problem in a Runtime event which i did not included above.

2.)when i use

Runtime:addeventListner(“enterframe”,functionname) in game.lua.

after i lose and enter gameover.lua and restart the game.

my background moves at twice its speed.

You are not adding “up” to the sceneGroup. Composer can’t manage that button for you. You also appear to be trying to do things with a button named “right” which you never create. I also don’t see where you ever attempt to remove the listener on “up”.

Rob

local composer=require("composer") local scene=composer.newScene() local centerX=display.contentWidth\*0.5; local centerY=display.contentHeight\*0.5; local physics=require("physics"); physics.start(); physics.setGravity(0,10); ------------ local up,local player,local ball; ------------ function scene:create(event) player=display.newImageRect("player.png",35,35); player.x=centerX-145; player.y=centerY-70; physics.addBody(player, "dynamic"); sceneGroup:insert(player); ball=display.newImageRect("ball.png",15,15); ball.x=100; ball.y=100; physics.addBody(ball, "dynamic"); sceneGroup:insert(ball); up=display.newImageRect("up.png",80,80); up.x=centerX+250; up.y=centerY+15; up.alpha=0.5; sceneGroup:insert(up); end local function upkey(event) if(event.phase=="began")then a1=transition.to(player,{time=100,y=player.y-150}); end end function scene:show(event) local sceneGroup=self.view local phase = event.phase if(phase=="will") then elseif(phase=="did")then up:addEventListener("touch",upkey);--adding the event listener end end -- there is also a function for collision function scene:hide(event) up:removeEventListener("touch",upkey);--remove event listener end function scene:destroy(event) transition.cancel(a1) end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ------------------------------------------ return scene

hello  Rob,

thanks for the reply and sorry i typed the wrong variable.
i tried to remove the event listener in function scene:hide.

scene:hide() gets called twice with phases like scene:show() does. You need to put the same kind of “if” test in scene:hide() that you have in scene:show().

Rob

hello rob,thanks for the reply i included “will” and “did” phase in  scene:hide() and now its  working :slight_smile: ,but sometimes

i get a nil value runtime error and after making some changes i get an error saying 

“attempt to compare nil with number”.

i created a sample to show what exactly happens but my .lua its not uploading.i dont know the exact reason of the error.

since its a different topic so i am posting it into another post.

You are not adding “up” to the sceneGroup. Composer can’t manage that button for you. You also appear to be trying to do things with a button named “right” which you never create. I also don’t see where you ever attempt to remove the listener on “up”.

Rob

local composer=require("composer") local scene=composer.newScene() local centerX=display.contentWidth\*0.5; local centerY=display.contentHeight\*0.5; local physics=require("physics"); physics.start(); physics.setGravity(0,10); ------------ local up,local player,local ball; ------------ function scene:create(event) player=display.newImageRect("player.png",35,35); player.x=centerX-145; player.y=centerY-70; physics.addBody(player, "dynamic"); sceneGroup:insert(player); ball=display.newImageRect("ball.png",15,15); ball.x=100; ball.y=100; physics.addBody(ball, "dynamic"); sceneGroup:insert(ball); up=display.newImageRect("up.png",80,80); up.x=centerX+250; up.y=centerY+15; up.alpha=0.5; sceneGroup:insert(up); end local function upkey(event) if(event.phase=="began")then a1=transition.to(player,{time=100,y=player.y-150}); end end function scene:show(event) local sceneGroup=self.view local phase = event.phase if(phase=="will") then elseif(phase=="did")then up:addEventListener("touch",upkey);--adding the event listener end end -- there is also a function for collision function scene:hide(event) up:removeEventListener("touch",upkey);--remove event listener end function scene:destroy(event) transition.cancel(a1) end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ------------------------------------------ return scene

hello  Rob,

thanks for the reply and sorry i typed the wrong variable.
i tried to remove the event listener in function scene:hide.

scene:hide() gets called twice with phases like scene:show() does. You need to put the same kind of “if” test in scene:hide() that you have in scene:show().

Rob

hello rob,thanks for the reply i included “will” and “did” phase in  scene:hide() and now its  working :slight_smile: ,but sometimes

i get a nil value runtime error and after making some changes i get an error saying 

“attempt to compare nil with number”.

i created a sample to show what exactly happens but my .lua its not uploading.i dont know the exact reason of the error.

since its a different topic so i am posting it into another post.