Quick Question about variable Timestep


(Jason Pickering) #1

Hey guys, so I have just a very quick question. I want something to happen after a specified amount of time using a timer. its basically a charge up attack, for the main character. The way I would normally do this, if using a fixed timestep, while the button is held down I would increase a timer variable. if the timer equals a specified amount then the player has charged enough. My question is how would I do this if the timestep could in theory jump past my timer goal. so if I want the player to flash briefly at 3 seconds how would I do that so it only happens once. would I need another Boolean variable and just use a greater then simple and then trip the variable. I just wonder if there is a better way to do this with out adding more variables.


(Zachary Lewis) #2

I’d probably have a variable on the character to determine if the character is or isn’t charged. Here’s a quick, untested class skeleton.

Basically, I’m assuming you’ll call player.attack() when the player first presses the button, then call player.attack(true) once the player releases the attack button.

class Player extends Entitiy
{
	protected var _charged:Boolean;
	protected var _attacking:Boolean;
	protected var _chargeTimer:Number;

	/** Time to fully charge (in seconds). */
	protected const CHARGE_TIME:Number = 3.0;

	/** Is the player charged? */
	public function get charged():Boolean { return _charged; }

	/** Is the player attacking? */
	public function get attacking():Boolean { return _attacking; }

	/** The length the player has been charging (in seconds). */
	public function get chargeTimer():Number { return _chargeTimer; }

	public function Player()
	{
		_charged = false;
	}

	public function attack(releaseAttack:Boolean = false):void
	{
		if (releaseAttack)
		{
			if (_charged)
			{
				// Perform charged attack.
			}
			else
			{
				// Perform standard attack.
			}

			_attacking = false;
		}
		else
		{
			// Begin charging.
			_attacking = true;
			_charged = false;
			_chargeTimer = 0.0;
		}

	}

	override public function update():void
	{
		// Only update the charging timer while the player isn't charged.
		if (_attacking && !_charged)
		{
			_chargeTimer += FP.elapsed;
			if (_chargeTimer >= CHARGE_TIME)
			{
				// Flash the player.
				_charged = true;
			}
		}
	}
}