You probably could use a timer for this, but I would think the easiest thing would be to use a Runtime “enterFrame” event listener where you have a variable that tracks the time of the touch and if it exceeds 5 seconds do something. This code is completely untested but should get you in the ball park.
local touchTimer = 0
local fliesToEat = 0
local startTimer = false
local function spawnFly()
fliesToEat = fliesToEat + 1
end
local function eatFly()
fliesToEat = fliesToEat - 1
if fliesToEat \< 0 then fliesToEat = 0 end
startTimer = false
end
local function shouldEatFly(event)
-- assuming 30 fps
if fliesToEat \> 0 and startTimer then
touchTimer = touchTimer + 1
if touchTimer \> (5 \* 30) then -- 5 seconds, 30 fps
eatFly()
end
end
end
frog = display.newImageRect("frog.png",64,64)
local function touchFrog(event)
if event.phase == "ended" then
touchTimer = 0
startTimer = true
end
return true -- very important!!!!!
end
frog:addEventListener("touch", touchFrog)
Runtime:addEventListener("enterFrame", shouldEatFly)
Or something like that.
Of course you have to be responsible for all the other parts to make this work.
EDIT: Something was **bugging** me about my solution in that if you have multiple flies, after 5 seconds the frog would start eating everything. I added a trap that kills the timer code after eating a fly that way the timer won’t start again until the frog as been tapped.
[import]uid: 19626 topic_id: 31403 reply_id: 125577[/import]