Fire Rate, and Checking Inputs


(John) #1

I am working on a platformer game at the moment, and I want the sprite to fire a bullet when the space key is pressed, but also when it is held down. I am also trying to make it so there is a regular delay between each bullet fired (but not on the first bullet fired). Does anyone here have any advice on how this can be done?

Thanks


(MartĂ­ Angelats i Ribera) #2

Before asking you should look at the documentation a bit. The multiple Input is already implemented (Input class [works statically]).

For the multiple bullets it’s pretty simple. Use FP.elapsed to know the time (in milliseconds) that has been eclipsed (updates every frame). Use a private variable, a if and the mod operator… If you can’t figure it out with this…


(John) #3

You’re toxic. I’m not coming back to these forums. bye!


(MartĂ­ Angelats i Ribera) #4

In the engine

//define the group of keys for shooting
Input.define("shootting", Key.SPACE, Key.DOWN);

In the entity

public var firePeriod:Number = 1; //inverse of fire rate in seconds (one second for shot in this case)

private var _t:Number = 0;
private var _pressed:Boolean = false;

override public function update():void
{
    //Detect the key
    if (Input.pressed("shooting"))
    {
        _pressed = true;
    }
    else if (Input.released("shooting"))
    {
        _pressed = false;
        _t = 0;
    }
    
    //Shoot automatically
    if (_pressed)
    {
        _t += FP.elapsed;
        if (_t >= firePeriod)
        {
            shoot(); //function that shoots the projectile.
            _t %= firePeriod;
        }
    }
}

Actually i thought that the documentation for this was good but it seems that i saw it somewere else. I didn’t ment to be rude but it seems that my comment ended up being it and i’m sorry about that (part of that is probably due the fact that english is not my first lenguage). I tried to tell you where you could find the information (which as said i was wrong about it).

But actually your reply wasn’t good ether. Even if i were a really toxic person, there are other people here so you should not be like that for a single reply. This may happen in other forums as well. There are a lot of people in the same place so the probability to find someone you don’t like is high.

PS: BTW, in FlashPunk you don’t work with Sprites, It works with Entities. I don’t know if you wrote it wrong or you are really using sprites.


(Zachary Lewis) #5

You can use a counter variable to keep track of time that has passed. Here’s a simple implementation that supports speed firing (fires as fast as you can press) and sustained firing (fires at a fixed rate when held).

public class ShootWorld extends World
{
  public var player:ShootingEntity;
  
  // If using fixed time step, these can be ints instead of Numbers.
  protected var _fireTimer:Number;
  protected const FIRE_RATE:Number = 0.5;
  
  override public function update():void
  {
    if (Input.pressed(Key.SPACE))
    {
      // Space was pressed this frame.

      // Reset the timer.
      _fireTimer = 0;

      // Fire a bullet.
      player.fire();
    }
    else if (Input.check(Key.SPACE))
    {
      // Space wasn't pressed this frame, but is being held.

      // Increment the counter based on time passed. If using a fixed
      // timestep, just add one.
      _fireTimer += FP.elapsed;

      if (_fireTimer >= FIRE_RATE)
      {
        // Reduce the timer by FIRE_RATE instead of setting to zero to
        // compensate for variable time.
        _fireTimer -= FIRE_RATE;

        // Fire a bullet.
        player.fire();
      }
    }
    
    super.update();
  }
}

@Copying The variable you’re looking for is FP.elapsed, not FP.eclipsed.


(MartĂ­ Angelats i Ribera) #6

WTF happend to my brain? LEL