Constantly Performing An Action When "touch" Is Down. How? Timer.performwithdelay?

Hi all,

I’m still learning Corona little by little, and my oobstacle this time is that I can’t use “timer.performWithDelay” properly. All I want to do is "as long as the screen is tapped, keep spawning and moving the ‘ball’ "

My code works unless I move the mouse. I mean it works as long as I keep the mouse down, but when I move it, it just spams “amItouching=true” and everything breaks.

My code is something like this;

local function onScreenTouch(event) amItouching = false if (event.phase == "ended") then amItouching = false print ( amItouching ) elseif (event.phase == "began") then amItouching = true print ( amItouching ) end if (amItouching==true) then cycle = timer.performWithDelay(600, doit, 77) else timer.cancel ( cycle ) end end Runtime:addEventListener( "touch", onScreenTouch )

I tried everything I can come up with. What to do now? :frowning:

Thanks in advance! :smiley:

Something like this? Apologies for the lack of formatting, I don’t have a code editor handy and this forum is awful (why does pasting code in from wordpad lose all formatting and why can’t I use the tab key for heaven’s sake??)

[lua]

local amItouching = false
 

local function onScreenTouch(event)
 

if event.phase ~= “moved” then

if (event.phase == “ended”) then

amItouching = false

print ( amItouching )

elseif (event.phase == “began”) then

amItouching = true

print ( amItouching )

end

if (amItouching==true) then

cycle = timer.performWithDelay( 600 , doit, 77 )

else

timer.cancel ( cycle )

end

end

end

Runtime:addEventListener( “touch”, onScreenTouch )

[/lua]

Ah, close but no cigar. Also you forgot an *end* perhaps? On the first if statement, or I might be doing something wrong.

By your way, it works when holding, but it stops working once you move the mouse (or tap) :frowning:

Thanks anyway tho :stuck_out_tongue:

Can’t see an end missing, the code I posted should work as is although I can’t test it as I haven’t got Corona on this machine. I’ll post it outside of lua tags as they seem to screw up the formatting. <_<

local amItouching = false

local function onScreenTouch(event)

if event.phase ~= “moved” then
 

if (event.phase == “ended”) then

 

amItouching = false

                        print ( amItouching )

 

elseif (event.phase == “began”) then
 

amItouching = true

print ( amItouching ) 

end

 

if (amItouching==true) then

cycle = timer.performWithDelay(600, doit, 77)

else

timer.cancel ( cycle )

end

 

end

end

 

Runtime:addEventListener( “touch”, onScreenTouch )

Thanks it is working! I become so emberessed when I fail to do *easy* stuff like that but you can imagine how much frustration you just saved me from. If it matters, I promise I’ll learn this enough to help people on Newbie forum at least :smiley:

Thanks again!

(And fixing the formatting issue when posting lua code would be really awesome!)

Something like this? Apologies for the lack of formatting, I don’t have a code editor handy and this forum is awful (why does pasting code in from wordpad lose all formatting and why can’t I use the tab key for heaven’s sake??)

[lua]

local amItouching = false
 

local function onScreenTouch(event)
 

if event.phase ~= “moved” then

if (event.phase == “ended”) then

amItouching = false

print ( amItouching )

elseif (event.phase == “began”) then

amItouching = true

print ( amItouching )

end

if (amItouching==true) then

cycle = timer.performWithDelay( 600 , doit, 77 )

else

timer.cancel ( cycle )

end

end

end

Runtime:addEventListener( “touch”, onScreenTouch )

[/lua]

Ah, close but no cigar. Also you forgot an *end* perhaps? On the first if statement, or I might be doing something wrong.

By your way, it works when holding, but it stops working once you move the mouse (or tap) :frowning:

Thanks anyway tho :stuck_out_tongue:

Can’t see an end missing, the code I posted should work as is although I can’t test it as I haven’t got Corona on this machine. I’ll post it outside of lua tags as they seem to screw up the formatting. <_<

local amItouching = false

local function onScreenTouch(event)

if event.phase ~= “moved” then
 

if (event.phase == “ended”) then

 

amItouching = false

                        print ( amItouching )

 

elseif (event.phase == “began”) then
 

amItouching = true

print ( amItouching ) 

end

 

if (amItouching==true) then

cycle = timer.performWithDelay(600, doit, 77)

else

timer.cancel ( cycle )

end

 

end

end

 

Runtime:addEventListener( “touch”, onScreenTouch )

Thanks it is working! I become so emberessed when I fail to do *easy* stuff like that but you can imagine how much frustration you just saved me from. If it matters, I promise I’ll learn this enough to help people on Newbie forum at least :smiley:

Thanks again!

(And fixing the formatting issue when posting lua code would be really awesome!)