Timers in enterFrame listeners

At the moment, I have a joystick (SSK2) that allows me to use an enterFrame event for its touch inputs. 

This joystick controls the direction in which my character shoots, however, the character is supposed to shoot once every 250 ms. 

How could I have it so that instead of my character shooting once every 16.6666 ms, it shoots every 250 ms? When I tried using timers, they would just stack.

Thank you.

[lua]

local shootTimer = system.getTimer()
local frequency = 250

– put this inside enterFrame listener

if system.getTimer() - shootTimer >= frequency then

– shoot
shootTimer = system.getTimer()
end

[/lua]

I understand what you’re trying to say here, but you’ve mixed up a few things.  No prob.  This is how it works when using SSK, specifically any of the easy input helpers

  1. Add the easy input helper to your game:

    ssk.easyInputs.oneStick.create( someGroup )

  2. Create your player object and attach a listener for the joystick to it.

    local player = display.newCircle( 10, 10, 10 ) – add a body physics.addBody(player) – set some flags player.maxThrustMag = 5 player.firing = false player.firePeriod = 100 – 10 times per second max – Add the joystick listener – In this example, the player: – >> fires whenever the joystick is in use – >> faces in the direction of the joystick, but not instantly – >> thrusts – The work for all this is done in enterFrame – Setting flags and magnitudes is done here – function player.onJoystick( self, event ) self.firing = true self.targetAngle = event.angle self.curThrustMag = event.percent * self.maxThrustMag if (event.phase == “ended” or event.phase == “cancelled”) then self.targetAngle = self.rotation self.curThrustMag = 0 self.firing = false end end; listen( “onJoystick”, player )

  3. Add the enterFrame listener to do all the firing, thrusting, and facing work:

    function player.enterFrame( self, event ) ssk.actions.face( self, { angle = self.targetAngle, rate = 360 } ) if( self.curThrustMag > 0 ) then ssk.actions.movep.thrustForward( self, { rate = self.curThrustMag } ) end if( self.firing ) then self:fire() end end; listen( “enterFrame”, player )

  4. Add a helper function to the player to ‘fire’

    function player.fire( self ) local curT = system.getTimer() – On the first call, self.lastT is set to curT: self.lastT = self.lastT or curT – How long has it been since the last time we fired? local dt = curT - self.lastT – If it has been longer than our fire period, then fire again if( dt < self.firePeriod ) then return end – self.lastT = curT – Add your own code to make bullets and set them in motion here end

  5. Add a finalize listener to clean up (using SSK features):

    function player.finalize( self ) ignoreList( { “enterFrame”, “onJoystick” }, self ) end; player:addEventListener(“finalize”)

Thank you @roaminggamer, your approach worked perfectly. 

[lua]

local shootTimer = system.getTimer()
local frequency = 250

– put this inside enterFrame listener

if system.getTimer() - shootTimer >= frequency then

– shoot
shootTimer = system.getTimer()
end

[/lua]

I understand what you’re trying to say here, but you’ve mixed up a few things.  No prob.  This is how it works when using SSK, specifically any of the easy input helpers

  1. Add the easy input helper to your game:

    ssk.easyInputs.oneStick.create( someGroup )

  2. Create your player object and attach a listener for the joystick to it.

    local player = display.newCircle( 10, 10, 10 ) – add a body physics.addBody(player) – set some flags player.maxThrustMag = 5 player.firing = false player.firePeriod = 100 – 10 times per second max – Add the joystick listener – In this example, the player: – >> fires whenever the joystick is in use – >> faces in the direction of the joystick, but not instantly – >> thrusts – The work for all this is done in enterFrame – Setting flags and magnitudes is done here – function player.onJoystick( self, event ) self.firing = true self.targetAngle = event.angle self.curThrustMag = event.percent * self.maxThrustMag if (event.phase == “ended” or event.phase == “cancelled”) then self.targetAngle = self.rotation self.curThrustMag = 0 self.firing = false end end; listen( “onJoystick”, player )

  3. Add the enterFrame listener to do all the firing, thrusting, and facing work:

    function player.enterFrame( self, event ) ssk.actions.face( self, { angle = self.targetAngle, rate = 360 } ) if( self.curThrustMag > 0 ) then ssk.actions.movep.thrustForward( self, { rate = self.curThrustMag } ) end if( self.firing ) then self:fire() end end; listen( “enterFrame”, player )

  4. Add a helper function to the player to ‘fire’

    function player.fire( self ) local curT = system.getTimer() – On the first call, self.lastT is set to curT: self.lastT = self.lastT or curT – How long has it been since the last time we fired? local dt = curT - self.lastT – If it has been longer than our fire period, then fire again if( dt < self.firePeriod ) then return end – self.lastT = curT – Add your own code to make bullets and set them in motion here end

  5. Add a finalize listener to clean up (using SSK features):

    function player.finalize( self ) ignoreList( { “enterFrame”, “onJoystick” }, self ) end; player:addEventListener(“finalize”)

Thank you @roaminggamer, your approach worked perfectly.