Touch event keeps looping

The problem was needs to use 

if (event.phase == “will”) condition and I didn’t use it. Just a common mistake.

-- "scene:show()" function scene:show( event )     local sceneGroup = self.view          if(event.phase == "will") then     -- I didn't put Event Listener in event.phase == "will" condition -- This is correct answer, on previous I was calling addEventListener without using if (event.phase == "will") condition     Runtime:addEventListener("enterFrame", methodToDo)     end  end

Add

return true

At the end of your startGame function

:slight_smile:

I also suggest using the “ended” phase.  Otherwise you risk triggering an ended phase for another button if that button is displayed in the same position.

This is the problem:

-- "scene:show()" function scene:show( event ) local sceneGroup = self.view -- Touch the background and goes to startGame function to start the game background:addEventListener("touch", startGame) print("Play Scene: Touch the screen.") end

That function is called twice and so you get TWO listeners.

Do this instead:

-- "scene:show()" function scene:show( event ) local sceneGroup = self.view if( event.phase == "did" ) then -- Touch the background and goes to startGame function to start the game background:addEventListener("touch", startGame) print("Play Scene: Touch the screen.") end end

Go fix the hide() listener too as it is ALSO called twice.

Inside the ended phase, do I need to remove something or?? Currently I am debugging and I don’t know what is the error now :frowning:

Note: If the fix I gave is unclear (re: hide() and show()) go take a look at the docs on these functions as they have two phases and you need to understand this to use them properly:

Note: Don’t feel bad BTW.  This is a common mistake.

Thanks bro. I fixed the bug now. :slight_smile: Yes checking the API is better. By the way, thank you so much :slight_smile:

I had to learn this the hard way (By multiple people getting upset/mad at me) but please don’t post a post and then after it getting answered just delete it or like you did (Add a smiley face). People in the future with this problem will come to find an answer and will only see your smiley face and i don’t think that will help them much. If you are trying to hide your code I totally understand but at the same time no one needs your function with a couple event.phases in it (No offence). Of course if you need help with an algorithm that you’ve been working on for an extended period of time and posted it trying to get help on it and plan on selling it in the future then I’m sure we can understand if you delete that. I haven’t seen your code so i could be out of this world wrong on this. 

I also see you’re new? Have fun coding and good luck on your game!

Bro, I am sorry about it. I didn’t notice and the problem is I didn’t use

event.phase == “will” and event.phase == “did” so my eventListeners are called 2 times. Just a common mistake though.

-- "scene:show()" function scene:show( event )     local sceneGroup = self.view          if(event.phase == "will") then     -- I didn't put Event Listener in event.phase == "will" condition -- This is correct answer, on previous I was calling addEventListener without using if (event.phase == "will") condition     Runtime:addEventListener("enterFrame", methodToDo)     end  end

function scene:show( event ) local phase = event.phase if ( phase == "will" ) then -- Called when the scene is still off screen (but is about to come on screen). elseif ( phase == "did" ) then -- Called when the scene is now on screen. -- Insert code here to make the scene come alive. -- Example: start timers, begin animation, play audio, etc. end end scene:addEventListener( "show" )

Add

return true

At the end of your startGame function

:slight_smile:

I also suggest using the “ended” phase.  Otherwise you risk triggering an ended phase for another button if that button is displayed in the same position.

This is the problem:

-- "scene:show()" function scene:show( event ) local sceneGroup = self.view -- Touch the background and goes to startGame function to start the game background:addEventListener("touch", startGame) print("Play Scene: Touch the screen.") end

That function is called twice and so you get TWO listeners.

Do this instead:

-- "scene:show()" function scene:show( event ) local sceneGroup = self.view if( event.phase == "did" ) then -- Touch the background and goes to startGame function to start the game background:addEventListener("touch", startGame) print("Play Scene: Touch the screen.") end end

Go fix the hide() listener too as it is ALSO called twice.

Inside the ended phase, do I need to remove something or?? Currently I am debugging and I don’t know what is the error now :frowning:

Note: If the fix I gave is unclear (re: hide() and show()) go take a look at the docs on these functions as they have two phases and you need to understand this to use them properly:

Note: Don’t feel bad BTW.  This is a common mistake.

Thanks bro. I fixed the bug now. :slight_smile: Yes checking the API is better. By the way, thank you so much :slight_smile:

I had to learn this the hard way (By multiple people getting upset/mad at me) but please don’t post a post and then after it getting answered just delete it or like you did (Add a smiley face). People in the future with this problem will come to find an answer and will only see your smiley face and i don’t think that will help them much. If you are trying to hide your code I totally understand but at the same time no one needs your function with a couple event.phases in it (No offence). Of course if you need help with an algorithm that you’ve been working on for an extended period of time and posted it trying to get help on it and plan on selling it in the future then I’m sure we can understand if you delete that. I haven’t seen your code so i could be out of this world wrong on this. 

I also see you’re new? Have fun coding and good luck on your game!

Bro, I am sorry about it. I didn’t notice and the problem is I didn’t use

event.phase == “will” and event.phase == “did” so my eventListeners are called 2 times. Just a common mistake though.

-- "scene:show()" function scene:show( event )     local sceneGroup = self.view          if(event.phase == "will") then     -- I didn't put Event Listener in event.phase == "will" condition -- This is correct answer, on previous I was calling addEventListener without using if (event.phase == "will") condition     Runtime:addEventListener("enterFrame", methodToDo)     end  end

function scene:show( event ) local phase = event.phase if ( phase == "will" ) then -- Called when the scene is still off screen (but is about to come on screen). elseif ( phase == "did" ) then -- Called when the scene is now on screen. -- Insert code here to make the scene come alive. -- Example: start timers, begin animation, play audio, etc. end end scene:addEventListener( "show" )