[Resolved] Touch listener being called twice.

My third day with Corona and I’ve pretty much got the beginnings of a game up and running but I’m still a bit woolly-headed with some of the event stuff.

I’ve got this cascading thing which handles a level-complete screen.

It appears.
Waits for a touch.
It Disappears.

Here’s the code:

local function levelCompleteExit()  
 levelCompleteScreen.isVisible = false  
 print("flag4")  
 if(levelNum \< numLevels) then  
 deleteLevel()  
 initLevel()  
 else  
 gameOver()   
 end  
 return true  
end  
  
local function levelCompleteTouched ( event )  
 print(event.phase)  
 print("flag2")  
 if event.phase == "ended" then  
 print("flag3")  
 local fadeLevelComplete = transition.to( levelCompleteScreen, { alpha=0.0, xScale=2.0, yScale=2.0, time=500, onComplete=levelCompleteExit } )  
 levelCompleteScreen.removeEventListener("touch", levelCompleteTouched)  
 end  
return true  
  
end  
  
function levelComplete()  
 print("flag1")  
 levelCompleteScreen.alpha = 0  
 levelCompleteScreen.isVisible = true  
 levelCompleteScreen:toFront()  
 levelCompleteScreen.xScale = 2.0; levelCompleteScreen.yScale = 2.0  
 local showLevelComplete = transition.to( levelCompleteScreen, { alpha=1.0, xScale=1.0, yScale=1.0, time=500 } )  
 levelCompleteScreen:addEventListener( "touch", levelCompleteTouched )  
end  

levelCompleteTouched appears to be called more than once with the ‘ended’ phase and I really don’t want it to be.

I figured that the removal of the event listener from within the listener itself might be a bit shaky but with or without it, it seems to make no difference.

Any suggestions as to why my touch event is getting triggered twice?
[import]uid: 25233 topic_id: 5568 reply_id: 305568[/import]

Rob,

Just in case it’s not a forum typo; line 19 has a “.” instead of a “:” in the removeEventListener statement.

If that’s not the issue:

Are you sure you don’t have a second touch listener firing or perhaps levelComplete is being called twice?

Since you’re not using the “moved” or “began” phases perhaps you would be better off using the “tap” listener instead of “touch”. This will fire your function directly without having to check for different states. [import]uid: 11393 topic_id: 5568 reply_id: 19364[/import]

Yes, that’s exactly what it was bedhouin!

Many thanks for that one and also the tip about using ‘tap’ instead of ‘touch’. [import]uid: 25233 topic_id: 5568 reply_id: 19914[/import]

Thanks friend. That solved my problem.
:slight_smile: [import]uid: 139976 topic_id: 5568 reply_id: 110411[/import]