Timed loops

Hello,

     I am working on a basic arcade style shooting game that requires machine gun functionality.  I am having issues looping a shoot function with variable time between shots.  I am using an event function that sets a boolean variable to true when the touch event.phase = began and sets it to false when the event ends.  I am looping while the boolean is true.  The loop calls a separate shooting function.  This technique doesn’t give me errors, it just locks up the simulator.   Any help would be appreciated!  Thanks!

Never mind, I found a solution.  However, in the process, I found another problem.  I got rid of the while loop and just looped using a timer that runs indefinitely.  I know how to cancel the timer when my ammo variable runs out, but I can’t get it to cancel when the touch event ends.  Does anyone know how to share a touch event’s phase variable between functions?

You need to save the return value from timer.performWithDelay to a variable that you can then use to call timer.cancel().

local myTimer = timer.performWithDelay(100, fireWeapon, 0) – or whatever you setup.

 

timer.cancel(myTimer)

For your last question, I think you need to provide more details as to what you’re trying to accomplish.

Okay, first of all, the whole system is working now; however, I don’t completely understand why.  My code looks something like this:

local ammo = 20

function shootMachineGun()

–play sounds and create muzzle flare graphics

     if( ammo == 0) then

     timer.cancel(shootTimer)

     end

end

function shootGeneral(event)

if(currentWeapon == “machineGun”) then

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

          machineGunTimer = timer.performWithDelay(100, shootMachineGun, 0)

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

          timer.cancel(machineGunTimer)

     end

end

Runtime:addEventListener(“touch”, shootGeneral)

What I do not understand is why the elseif statement in the shootGeneral function still works even after I have called shootMachineGun.

Thanks for the help!

Looks like it should work, stopping the timer if you let up before you fire your 20 rounds.  Though should this:

     timer.cancel(shootTimer)

be

     timer.cancel(machineGunTimer)

???

Never mind, I found a solution.  However, in the process, I found another problem.  I got rid of the while loop and just looped using a timer that runs indefinitely.  I know how to cancel the timer when my ammo variable runs out, but I can’t get it to cancel when the touch event ends.  Does anyone know how to share a touch event’s phase variable between functions?

You need to save the return value from timer.performWithDelay to a variable that you can then use to call timer.cancel().

local myTimer = timer.performWithDelay(100, fireWeapon, 0) – or whatever you setup.

 

timer.cancel(myTimer)

For your last question, I think you need to provide more details as to what you’re trying to accomplish.

Okay, first of all, the whole system is working now; however, I don’t completely understand why.  My code looks something like this:

local ammo = 20

function shootMachineGun()

–play sounds and create muzzle flare graphics

     if( ammo == 0) then

     timer.cancel(shootTimer)

     end

end

function shootGeneral(event)

if(currentWeapon == “machineGun”) then

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

          machineGunTimer = timer.performWithDelay(100, shootMachineGun, 0)

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

          timer.cancel(machineGunTimer)

     end

end

Runtime:addEventListener(“touch”, shootGeneral)

What I do not understand is why the elseif statement in the shootGeneral function still works even after I have called shootMachineGun.

Thanks for the help!

Looks like it should work, stopping the timer if you let up before you fire your 20 rounds.  Though should this:

     timer.cancel(shootTimer)

be

     timer.cancel(machineGunTimer)

???