home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mobiclic 161
/
MOBICLIC161.ISO
/
pc
/
DATA
/
DSS161
/
DSS161_00
/
DSS161_00.swf
/
scripts
/
dss161
/
engineaddons
/
timers
/
MainTimer.as
next >
Wrap
Text File
|
2014-01-15
|
5KB
|
198 lines
package dss161.engineaddons.timers
{
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.utils.getTimer;
public class MainTimer
{
protected var _timer:Timer;
protected var _delay:Number;
protected var _repeatCount:Number;
protected var _isSleeping:Boolean = false;
protected var _listeners:Vector.<TimerChild>;
protected var _listenersLength:int = 0;
protected var _running:Boolean = false;
protected var _idUnique:int = 1;
public var time:Number = 0;
protected var _tempChild:TimerChild;
protected var timerToWake:Boolean = false;
public function MainTimer()
{
this._timer = new Timer(1);
this._listeners = new Vector.<TimerChild>();
super();
}
public function addFromObj(param1:Object) : TimerChild
{
var _loc2_:TimerChild = new TimerChild(this);
++this._idUnique;
_loc2_.id = param1.id == undefined ? "" : param1.id + this._idUnique;
_loc2_.delay = param1.delay;
_loc2_.loop = param1.loop == undefined ? 0 : int(param1.loop);
_loc2_.func = param1.onDelay;
_loc2_._running = false;
return _loc2_;
}
public function add(param1:TimerChild) : void
{
if(this._listeners.indexOf(param1) == -1)
{
this._listeners.push(param1);
this._listenersLength = this._listeners.length;
}
this.start();
}
public function removeAllChilds() : void
{
this._listeners.length = this._listenersLength = 0;
}
public function removeChild(param1:TimerChild) : void
{
if(this._listeners.indexOf(param1) != -1)
{
this._listeners.splice(this._listeners.indexOf(param1),1);
this._listenersLength = this._listeners.length;
}
}
public function start() : void
{
if(this._running)
{
return;
}
this._running = true;
this._timer.addEventListener(TimerEvent.TIMER,this._timeHandler);
this.resetAll();
this._timer.start();
}
public function resetAll() : void
{
for each(this._tempChild in this._listeners)
{
this._tempChild.timeStart = getTimer();
this._tempChild.timeElapsed = 0;
}
}
public function stopAllChildren() : void
{
for each(this._tempChild in this._listeners)
{
this._tempChild.stop();
}
}
protected function _timeHandler(param1:TimerEvent) : void
{
this.time = getTimer();
for each(this._tempChild in this._listeners)
{
if(this._running == false)
{
return;
}
if((this._tempChild.timeElapsed = this.time - this._tempChild.timeStart) > this._tempChild.delay)
{
this._tempChild.timeStart = this.time;
this._tempChild.timeElapsed = 0;
this._tempChild.func(this._tempChild);
if(this._tempChild.loop > 1)
{
--this._tempChild.loop;
}
else if(this._tempChild.loop == 1)
{
this._tempChild.stop();
}
}
}
if(this._listeners.length == 0)
{
this.stop();
}
param1.updateAfterEvent();
}
public function get numChildren() : int
{
return this._listenersLength;
}
public function get running() : Boolean
{
return this._running;
}
public function stop() : void
{
if(this._running == false)
{
return;
}
this._running = false;
this._timer.removeEventListener(TimerEvent.TIMER,this._timeHandler);
this._timer.stop();
}
public function sleep() : void
{
if(this._isSleeping)
{
return;
}
this._isSleeping = true;
if(this._timer.running)
{
this.timerToWake = true;
this.stop();
}
this.stop();
}
public function wake() : void
{
var offsetTime:Number = NaN;
if(this._isSleeping == false)
{
return;
}
this._isSleeping = false;
if(this.timerToWake)
{
offsetTime = getTimer() - this.time;
this._listeners.forEach(function(param1:*, param2:int, param3:Vector.<TimerChild>):void
{
param1.timeStart + offsetTime;
},this);
this.start();
}
}
public function destroy() : void
{
this.stop();
this.removeAllChilds();
}
}
}