Skip to content

Instantly share code, notes, and snippets.

@alumican
Created September 3, 2014 09:21
Show Gist options
  • Save alumican/4167fdee5c2b5c982216 to your computer and use it in GitHub Desktop.
Save alumican/4167fdee5c2b5c982216 to your computer and use it in GitHub Desktop.
AS3 Timer based on getTimer
package util
{
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.TimerEvent;
import flash.utils.getTimer;
/**
* This is the timer class based on getTimer().
* You can use this when publishing video (.mov), because The Flash Professional CC has an issue that native Timer is NOT works in publishing video.
*/
public class Timer extends EventDispatcher
{
//----------------------------------------------------------
//
// Constructor
//
//----------------------------------------------------------
/**
* Constructs a new Timer object with the specified delay and repeatCount states.
* The enterFrameDispatcher is an instance of EventDispatcher class that is be able to dispatch Event.ENTER_FRAME.
*/
public function Timer(enterFrameDispatcher:DisplayObject, delay:Number, repeatCount:int = 0):void
{
_delay = delay;
_repeatCount = repeatCount;
_enterFrameDispatcher = enterFrameDispatcher;
_running = false;
reset();
}
//----------------------------------------------------------
//
// Property
//
//----------------------------------------------------------
//--------------------------------------
// currentCount
//--------------------------------------
private var _currentCount:int;
/**
* [read-only] The total number of times the timer has fired since it started at zero.
*/
public function get currentCount():int
{
return _currentCount;
}
//--------------------------------------
// delay
//--------------------------------------
private var _delay:Number;
/**
* The delay, in milliseconds, between timer events.
*/
public function get delay():Number
{
return _delay;
}
public function set delay(value:Number):void
{
_delay = value;
}
//--------------------------------------
// repeatCount
//--------------------------------------
private var _repeatCount:int;
/**
* The total number of times the timer is set to run.
*/
public function get repeatCount():int
{
return _repeatCount;
}
public function set repeatCount(value:int):void
{
_repeatCount = value;
}
//--------------------------------------
// running
//--------------------------------------
private var _running:Boolean;
/**
* [read-only] The timer's current state; true if the timer is running, otherwise false.
*/
public function get running():Boolean
{
return _running;
}
/**
* @private
*/
private var _enterFrameDispatcher:DisplayObject;
private var _startTime:Number;
//----------------------------------------------------------
//
// Function
//
//----------------------------------------------------------
/**
* Starts the timer, if it is not already running.
*/
public function start():void
{
if (_running)
return;
_running = true;
_enterFrameDispatcher.addEventListener(Event.ENTER_FRAME, _enterFrameHandler);
_startTime = getTimer();
}
/**
* Stops the timer.
*/
public function stop():void
{
if (!_running)
return;
_running = false;
_enterFrameDispatcher.removeEventListener(Event.ENTER_FRAME, _enterFrameHandler);
}
/**
* Stops the timer, if it is running, and sets the currentCount property back to 0, like the reset button of a stopwatch.
*/
public function reset():void
{
stop();
_currentCount = 0;
_startTime = -1;
}
/**
* @private
*/
private function _enterFrameHandler(event:Event):void
{
var currentTime:Number = getTimer();
if (currentTime >= _startTime + _delay)
{
++_currentCount;
dispatchEvent(new TimerEvent(TimerEvent.TIMER));
if (_repeatCount != 0 && _currentCount >= _repeatCount)
{
stop();
dispatchEvent(new TimerEvent(TimerEvent.TIMER_COMPLETE));
}
else
_startTime = currentTime;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment