0% found this document useful (0 votes)
189 views3 pages

FLX Timer

This document defines the FlxTimer class which is used to create timers in Flixel. The class contains properties and methods for running, stopping, and updating timers. When a timer completes, it dispatches a signal notifying any listeners. Timers can be added to the game timer system via FlxG to trigger regularly during the game loop.

Uploaded by

Marshall Mattle
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
189 views3 pages

FLX Timer

This document defines the FlxTimer class which is used to create timers in Flixel. The class contains properties and methods for running, stopping, and updating timers. When a timer completes, it dispatches a signal notifying any listeners. Timers can be added to the game timer system via FlxG to trigger regularly during the game loop.

Uploaded by

Marshall Mattle
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

package org.

flixel
{
import org.osflash.signals.Signal;
public class FlxTimer
{
protected var _delay:Number;
protected var _timeLeft:Number;
protected var _running:Boolean;
protected var _dispatchOnStart:Boolean;
protected var _complete:Signal;
/**
* The timer's current state; true if the timer is running, othe
rwise false.
*/
public function get running():Boolean
{
return _running;
}
public function get delay():Number
{
return _delay;
}
public function set delay(value:Number):void
{
_delay = value;
stop();
}
/**
* Creates a FlxTimer object. Call add() to add a method to be c
alled by the timer.
* Remember to call FlxG.addTimer(), or addTimer() from FlxState
or anything that inherits from FlxObject.
* The best way to add a new timer is something like this:
* var timer:FlxTimer = FlxG.addTimer(new FlxTimer(1));
*
* @param delay The delay, in se
conds, of the timer.
* @param dispatchOnStart Whether this timer shoul
d dispatch its listener when the start() method is called.
*/
public function FlxTimer(delay:Number, dispatchOnStart:Boolean =
false)
{
_delay = delay;
_timeLeft = delay;
_dispatchOnStart = dispatchOnStart;
_complete = new Signal();
_running = false;
}
/**
* Starts the timer. If the timer is started, it will be restart
ed.
*
* @return This FlxTimer instance. Nice for chaining stuff
together.
*/
public function start():FlxTimer
{
if (_running)
{
stop();
start();
}
else
{
if (_dispatchOnStart)
_complete.dispatch();
_running = true;
}
return this;
}
/**
* Stops the timer.
*
* @return This FlxTimer instance. Nice for chaining stuff
together.
*/
public function stop():FlxTimer
{
_running = false;
_timeLeft = _delay;
return this;
}
public function update():void
{
if (_running)
{
_timeLeft -= FlxG.elapsed;
if (_timeLeft <= 0)
{
_complete.dispatch();
_timeLeft = _delay;
}
}
}
public function toString():String
{
return '[FlxTimer: delay = ' + _delay.toString() + ']';
}
// DELEGATE org.osflash.signals.Signal \\
public function add(listener:Function):Function
{
return _complete.add(listener);
}
public function addOnce(listener:Function):Function
{
return _complete.addOnce(listener);
}
public function remove(listener:Function):Function
{
return _complete.remove(listener);
}
public function removeAll():void
{
_complete.removeAll();
}
}
}

You might also like