Preventing multiple clicks on buttons

Hi

I raised a query about a month ago but stopping click event bubbling through to other click events (http://developer.anscamobile.com/forum/2012/01/29/z-index-touch-or-tap) which resolved my issue to a point.

While my game is in testing it has come to light that if someone hammers a button it crashes or has undesirable effects. How can I have it so that the user can click once and only once. I tried removing the listener on click so it wouldn’t take another click but unless I implemented it incorrectly this didn’t seem to work.

To explain the button clicks start off a transition in storyboard to move to another scene so it doesn’t matter that its once and then destroyed.

How is this best handled please, as I tried numTaps with a tap listener too but this of course only works for a very small duration? [import]uid: 103970 topic_id: 23528 reply_id: 323528[/import]

Set up a variable to store whether the user is allowed to click the button. If your button object is called button:
[lua]button.canClick = true[/lua]

Then in your touch event function make sure you use the event.phase parameter:

[lua]local touchButton = function (event)

local obj = event.target

if event.phase == “ended” and obj.canClick == true then

obj.canClick = false

– your transition code

end

end[/lua] [import]uid: 93133 topic_id: 23528 reply_id: 94398[/import]

Also be sure to add

return true  

at the end of the function nick posted above [import]uid: 84637 topic_id: 23528 reply_id: 94462[/import]

@Danny - (this is in regards to my post here: http://developer.anscamobile.com/forum/2012/03/18/functions-and-return-types)
What does the ‘return true’ accomplish in this scenario? [import]uid: 132483 topic_id: 23528 reply_id: 95431[/import]

afaik, returns true means that the touch event won’t get propagated further [import]uid: 90610 topic_id: 23528 reply_id: 95438[/import]

@jon : If you do return true in a touch function then dingo’s advice is correct.

[import]uid: 84637 topic_id: 23528 reply_id: 95479[/import]

so once it returns true the touch event is disabled? [import]uid: 132483 topic_id: 23528 reply_id: 95619[/import]

@Jon, kind of but no.

Basically putting return true at the end of your touch function is like saying :

“Ok, the function has run, so we will now exit the function and not execute its code again until it has been touched again”

[import]uid: 84637 topic_id: 23528 reply_id: 95665[/import]