Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Timers in enterFrame listeners
Started by sdktester15 Jun 30 2018 08:33 AM

3 replies to this topic

Best Answer roaminggamer , 30 June 2018 - 09:03 AM

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")

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

sdktester15

[GLOBAL: userInfoPane.html]
sdktester15
  • Corona Geek

  • 1,140 posts
  • Corona SDK

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.



[TOPIC: post.html]
#2

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,456 posts
  • Corona SDK

local shootTimer = system.getTimer()
local frequency = 250
-- put this inside enterFrame listener
if system.getTimer() - shootTimer >= frequency then
  -- shoot
  shootTimer = system.getTimer()
end

  • sdktester15 likes this

[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,744 posts
  • Corona SDK

  Best Answer

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")



[TOPIC: post.html]
#4

sdktester15

[GLOBAL: userInfoPane.html]
sdktester15
  • Corona Geek

  • 1,140 posts
  • Corona SDK

Thank you @roaminggamer, your approach worked perfectly. 


  • roaminggamer likes this


[topic_controls]
[/topic_controls]