Urgent help: Touch event is driving me nuts!

Help!

I am using a group with a button that takes me to another group when the button is tapped. Everything works fine as long as I don’t tap to fast on the button. If I tap fast on the button, everything screws up.

Is there any way to deal with this, or do I have to redesign all my screens to a better approach?

I can’t remove the event listener, because it is created below the function. I guess that removing the listener would have been the best approach…or am I just tired?

  
local close = function( event )  
 if event.phase=="began" then  
 display.getCurrentStage():setFocus( btw )   
 elseif event.phase=="ended" then  
 --btn:removeEventListener("touch", close)  
 display.getCurrentStage():setFocus(nil)  
  
 local function remove()  
 welcomeGroup.isVisible=false;  
 showNextScreen()  
 end  
  
 transition.to(welcomeGroup,{alpha=0,time=400,onComplete=remove})  
 return true;  
 end  
 end  
btn:addEventListener("touch", close)  
  

Joakim [import]uid: 81188 topic_id: 21922 reply_id: 321922[/import]

Why not try a “tap” event. You don’t have to worry about phases and such.

You could also set a flag on the began phase that indicates that it’s already pressed and if you get another press before the scene changes you can just ignore it.
something like:

local closing = false  
local close = function( event )  
 if closing then return true end  
 if event.phase=="began" then  
 closing = true  
 display.getCurrentStage():setFocus( btw )   
 elseif event.phase=="ended" then  
 display.getCurrentStage():setFocus(nil)  
 local function remove()  
 welcomeGroup.isVisible=false;  
 showNextScreen()  
 end  
 transition.to(welcomeGroup,{alpha=0,time=400,onComplete=remove})  
 return true;  
 end  
end  
btn:addEventListener("touch", close)  

[import]uid: 19626 topic_id: 21922 reply_id: 87153[/import]

Ok, I managed to sort this out. Your example works fine but I had to move down the closing=true after the “ended” phase. I added a print statement and it seems to work fine. Even if I managed to triple touch, one event was registred.

So I then removed the print statement, and it did not work again. Now I got several of event again. Lua is way to fast for it self sometimes. So the solution is to add a timer that holds the code for a millisecond. Then it works!

Big thanks, Joakim
[import]uid: 81188 topic_id: 21922 reply_id: 87158[/import]

use the

local close
close = function (event)

that way you can use the removeEventListener from within the function.

uncomment your line #5 and you should be in business [import]uid: 3826 topic_id: 21922 reply_id: 87173[/import]

@JayantV, can you provide me with a sample of code that shows me the exact syntax for removing the eventListener. As it is now I am getting a:

attempt to call method ‘removEventListener’ (a nil value)

Regards, Joakim [import]uid: 81188 topic_id: 21922 reply_id: 87234[/import]

Are you sure that you did not misspell? you say removEventListener instead of removeEventListener

hoping that the misspelling is no the problem,

[code]
local welcomeGroup = display.newGroup()
local btn = display.newImage(welcomeGroup,“image.png”)

local function showNextScreen()
print(“Next Screen”)
end

local close

function close ( event )
if event.phase==“began” then
display.getCurrentStage():setFocus( btw )
elseif event.phase==“ended” then
btn:removeEventListener(“touch”, close)
display.getCurrentStage():setFocus(nil)

local function remove()
welcomeGroup.isVisible=false;
showNextScreen()
end

transition.to(welcomeGroup,{alpha=0,time=400,onComplete=remove})
return true
end
end

btn:addEventListener(“touch”, close)
[/code] [import]uid: 3826 topic_id: 21922 reply_id: 87274[/import]

Thanks, for your time!

No, there where no misspelling just in my post :wink:

I can’t remove the event listener, i am getting a:

ERROR: nil key supplied for property lookup.

I am not sure why, I think I used to do like this before in earlier releases. I am on 745 right now, if that could be the case? Really strange…

Joakim [import]uid: 81188 topic_id: 21922 reply_id: 87276[/import]

Maybe it is a new bug in the build 745, but just to be sure,
are you trying just the code that I send you or are you using portions of your code and mixing it up with the code above.

Try to just have a new file and copy/paste the code in that and run that, if it works then it is not the code above that is the issue but some other portion of your code. I hope you understand what I am trying to suggest here. [import]uid: 3826 topic_id: 21922 reply_id: 87280[/import]

You are right, it does work in a standalone project - but not in my module…I have to dig into this a little bit further.

Thanks, Joakim [import]uid: 81188 topic_id: 21922 reply_id: 87282[/import]

as I suspected…
in #4 you got attempt to call removeEventListener (nil value)
in #6 you got nil key supplied

you did not mention which line those errors related to nor the complete error message, that’s fine, you might be trying to save your real code.

From these two lines, it suggests that your error stems from the btn object, the way you have set up your btn object and the WelcomeGroup is the cause of the issue.

and to think more of why it could happen should reside in your showNextScene() function. You might be using StoryBoard or Director for scene management, the entire groups are removed and therefore it might interfere.

I am speculating blind as there is not much that you have provided for debugging or troubleshooting.

cheers and good luck on your troubleshooting. [import]uid: 3826 topic_id: 21922 reply_id: 87283[/import]

Yep, something strange going on here.

I am using storyboard for the application, but not for menus within the game, since the fact that I don’t want to reload the level. So this part of code is within a function in a module that I am referring to, from my game file.

I paste in the complete function here…

Joakim

[code]
–====================================================================================
– Gamestudio: xx xxxx xxxxxx
– Game: xx xxxx xxxxxx
– Author: Joakim Krassman
– Module: gameMenus.lua
– Purpose: Handling and creation of ingame menus.
– Creatiiondate: 2012-02-04
–====================================================================================
local myModule = {};
local t = {}

local welcomeGroup = nil;

function setTarget(t)
target = t;
end
myModule.setTarget = setTarget;
–====================================================================================
– PARENT ONLY - WELCOME SCREEN
–====================================================================================
local function createWelcome()

welcomeGroup = display.newGroup()
welcomeGroup.isVisible = false
welcomeGroup.alpha = 0

bg = display.newImage(“assets/Safari_iPad_GUI_PO_Welcome.png”,0,0)

bg.xScale = 1.8
bg.yScale = 1.8

welcomeGroup:insert(bg)

local btn = display.newImage(“assets/btn_close.png”,0,0)

welcomeGroup:insert(btn)

local close;

function close ( event )
if event.phase==“began” then
display.getCurrentStage():setFocus( btw )
elseif event.phase==“ended” then
btn:removeEventListener(“touch”, close)
display.getCurrentStage():setFocus(nil)

local function remove()
welcomeGroup.isVisible=false;
– Do whatever…
end

transition.to(welcomeGroup,{alpha=0,time=400,onComplete=remove})
return true
end
end

btn:addEventListener(“touch”, close)

return welcomeGroup;

end
myModule.createWelcome = createWelcome;

return myModule;
[/code] [import]uid: 81188 topic_id: 21922 reply_id: 87287[/import]

what’s btw in line 44? I think that is btn

you are creating the UI and returning it in a table/function. This seems fine except for the fact that since you set the WelcomeGroup to be not visible, it will never be seen or the contents of the same.

the error could well be in the way you are retaining the returned object/table and the way you are calling the function (maybe) [import]uid: 3826 topic_id: 21922 reply_id: 87290[/import]

The misspelling at line 44 is caused by my browser nifty spellchecking when I did some editing…

You are right, I am having another function to show the layer - but I cleaned up stuff that doesn’t affect this function.

I will try to sort this out…

Joakim
[import]uid: 81188 topic_id: 21922 reply_id: 87295[/import]