I’m writing a game in Corona using LUA Glider as my IDE and Level Director X for making the levels along with TexturePacker to create spritesheets.
I’m encountering a very unusual thing with a touch event handler.
As you can see in the code below, I have an object called “sign”. I’ve assigned a touch event handler to it that calls the function “flipSign”. The function flipSign checks if the touch event has ended and if so it increases a variable called “signDirection” by 1 and prints the value.
Every time I run the app and click the sign object in Corona Simulator the output prints:
1 2 3 4 5
Clicking the sign object again increases the variable another 5 times to 10. My understanding is the code should only increase the variable by 1 each time the sign is clicked.
Any help understanding why this is happening would be greatly appreciated.
Thanks!
local composer = require( "composer" ) local scene = composer.newScene() require("lib.LD\_LoaderX") physics = require ("physics") physics.start() local render = require ("render") -- ----------------------------------------------------------------------------------- -- Code outside of the scene event functions below will only be executed ONCE unless -- the scene is removed entirely (not recycled) via "composer.removeScene()" -- ----------------------------------------------------------------------------------- local myLevel = {} local signDirection = 0 local function flipSign( event ) if (event.phase == "ended") then signDirection = signDirection + 1 print(signDirection) end return true end -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- -- create() function scene:create( event ) -- Code here runs when the scene is first created but has not yet appeared on screen local sceneGroup = self.view myLevel= LD\_Loader:new(self.view) myLevel:loadLevel("level\_1") end -- show() function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is still off screen (but is about to come on screen) local sign = myLevel:getLayerObject("island\_4", "Sign\_2\_4").view sign:addEventListener ( "touch", flipSign ) elseif ( phase == "did" ) then -- Code here runs when the scene is entirely on screen end end -- hide() function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is on screen (but is about to go off screen) elseif ( phase == "did" ) then -- Code here runs immediately after the scene goes entirely off screen end end -- destroy() function scene:destroy( event ) local sceneGroup = self.view -- Code here runs prior to the removal of scene's view myLevel:removeLevel() myLevel = nil end -- ----------------------------------------------------------------------------------- -- Scene event function listeners -- ----------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ----------------------------------------------------------------------------------- return scene