How to make an event occur only if system remains idle for a given time period

Hi,

I want to trigger an event only if an object is not touched for 5 seconds.

for example, this is what i want to do:

I have a frog and a fly. On first touch, frog gets closer to the fly and if frog is not touched for another 5 seconds, the frog eats the fly.

The problem is, i want the frog to eat the fly ONLY if the frog is not touched for 5 seconds. But in my case, I start the timer after first touch, and the frog eats the fly after 5 seconds even if the frog is touched again.

Any help would be appreciated. [import]uid: 175611 topic_id: 31403 reply_id: 331403[/import]

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]

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]