removeEventListener

main.lua

local composer = require "composer" display.setStatusBar( display.HiddenStatusBar ) \_W = display.contentWidth \_H = display.contentHeight composer.gotoScene("game","fade", 300 )

game.lua

local composer = require "composer" local widget = require( "widget" ) display.setStatusBar( display.HiddenStatusBar ) local scene = composer.newScene() \_W = display.contentWidth \_H = display.contentHeight function scene:create( event ) local screenGroup= self.view local bg = display.newRect(111,300,500,800) bg:setFillColor(0.2,0.4,0.2) local button = display.newRect(100,300,40,40) local function goMenu(e) if e.phase == "began" then composer.gotoScene("game2","fade", 300 ) end end local function touch(e) if e.phase == "began" then f = display.newCircle(0,0,10) f.x , f.y = e.x ,e.y end end button:addEventListener("touch",goMenu) bg:addEventListener("touch",touch) end function scene:show( event ) end function scene:hide( event ) end function scene:destroy( event ) bg:removeEventListener("touch",touch) end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

game2.lua

local composer = require "composer" local widget = require( "widget" ) local scene = composer.newScene() \_W = display.contentWidth \_H = display.contentHeight function scene:create( event ) local screenGroup= self.view local bg2 = display.newRect(111,300,500,800) bg2:setFillColor(0.4,0.4,0.2) end function scene:show( event ) end function scene:hide( event ) end function scene:destroy( event ) end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

How to remove touch event listener ? 

I have the same question… how to remove it without change the scene…

These two functions:

local function goMenu(e)     if e.phase == "began" then         composer.gotoScene("game2","fade", 300 )     end end local function touch(e) if e.phase == "began" then      f = display.newCircle(0,0,10)      f.x , f.y = e.x ,e.y end end

exist inside the function scene:create() and you’ve declared them local here.  Inside of scene:destroy() to need access to “touch”, but it’s nil there since it’s declared inside of scene:create() as local.

To solve this problem, simply move the two functions above scene:create()

Rob

I have the same question… how to remove it without change the scene…

These two functions:

local function goMenu(e)     if e.phase == "began" then         composer.gotoScene("game2","fade", 300 )     end end local function touch(e) if e.phase == "began" then      f = display.newCircle(0,0,10)      f.x , f.y = e.x ,e.y end end

exist inside the function scene:create() and you’ve declared them local here.  Inside of scene:destroy() to need access to “touch”, but it’s nil there since it’s declared inside of scene:create() as local.

To solve this problem, simply move the two functions above scene:create()

Rob