Disable touch temporarily

What’s the best way to disable a touch button temporarily for like 2-3 seconds then reactivate that button? is there a way to do that?

I want to disable the touch button while the cannonball is active or flying through the air then reactivate the touch button after the ball is removed on a collision event.

Thank you.

[lua]local fireCannon = function ( event )

if event.phase == “ended” then

local cannonSteelBallTimer = timer.performWithDelay(50, cannonSteelBall, 1)
end
end

firebutton:addEventListener(“touch”, fireCannon) [import]uid: 12455 topic_id: 6066 reply_id: 306066[/import]

This sounds as simple as creating a true/false variable that tells you if you’re processing events for that button. In the event listener function, just test the flag before you process the event.

Another way would be to call removeEventListener() and then call addEventListener() again later.
[import]uid: 9659 topic_id: 6066 reply_id: 20783[/import]

lococo,

I am having a similar problem, however I am a total noob, so not sure how to implement what you said. I would like to allow a user to touch an object and play a sound, but while the sound is playing, not allow them to touch it and make it play the sound again (overlapping) until the first instance of the sound playing finishes. I tried adding removeEventListener() right after the tap, but that just disables it after the first tap. I also added another addEventistener(), but couldn’t figure out where to put it to make it work. Could you include a snippet example of where each goes?

Much appreciated! [import]uid: 32178 topic_id: 6066 reply_id: 20906[/import]

Thank you for your suggestions Lococo, the flag works.

jpot328,

this is what I did.
this works for my game, but i’m sure it will give you an idea.
[lua]-- flag
local ballinAir = false
local removeTouch = false
local resetTouch = false

– assume ball exist already…

local ballCollision = function (self, event)

if event.phase == “began” then
if event.other.objectName == “ground” then

self:removeSelf() – remove the ball
self = nil

ballinAir = false – the ball collides with another object so no longer in the air
resetTouch = true
end
end
end

firebutton.collision = lunchballTouch – assumming i have a firebutton setup already
firebutton:addEventListener(“touch”, lunchballTouch)

local lunchballTouch = function (event)

if event.phase == “ended” then
spawnball() – this is assuming i have the spawnball function already in my code somewhere
removeTouch = true
ballinAir = true – this tells me that the ball is flying through the air
end
end
local whateverEnterFrame = function ()

if (removeTouch) then
firebutton:removeEventListener(“touch”, lunchballTouch)
end

if (resetTouch) and not (ballinAir) then
firebutton:addEventListener(“touch”, lunchballTouch)
end
end

Runtime:addEventListener(“enterFrame”, whateverEnterFrame)
[import]uid: 12455 topic_id: 6066 reply_id: 20928[/import]