Time management for as3 mobile dev – Tick, Timeout and Interval

While working on my iPhone game project I realized that AIR 2.7 for IOS introduced bug with the Timer class and setTimeout.

So I wrote 3 static classes : Tick, Timeout, Interval class to bypass the problem. (With a big thx to @seraf_NSS for test, debug and enhancements)

To be simple Tick is a global ENTER_FRAME available from anywhere (including non-DisplayObject).
Then Timeout and Interval subscribes to when necessary.

To be optimized for mobile, all listeners are stored in vectors and no events are dispatched.

Tick use :

// create a tick
Tick.create(onTick);
 
// remove a tick
Tick.remove(onTick);
 
public function onTick():void {
	trace("tick");
}

Tick class
Tick demo class

Timeout use :

 
// create a timeout with a 300 ms delay
var id1:int = Timeout.create(onTimeout1, 300, 10);
 
// create a timeout with a 300 ms delay and 1 optionnal argument
var id2:int = Timeout.create(onTimeout2, 300, "hello");
 
// clear a timeout
Timeout.clear(id1);
 
public function onTimeout1():void {
	trace("onTimeout1");
}
 
public function onTimeout2(message:String):void {
	trace("onTimeout2", message);
}

Timout class
Timout demo class

Interval use :

 
// create a interval with a 300 ms delay repeated 10 times 
var id1:int = Interval.create(onInterval1, 300);
 
// create a timeout with a 300 ms delay repeated until we stop + 1 optionnal argument
var id2:int = Interval.create(onInterval2, 300, 0, "hello");
 
// clear an interval
Interval.clear(id1);
 
public function onInterval1():void {
	trace("onInterval1");
}
 
public function onInterval2(message:String):void {
	trace("onInterval2", message);
}

Interval class
Interval demo class

I am interested in your feedback on these classes and this article. Please point out my spelling mistakes, I think I’m better in Actionscript than in English…

Leave a Reply

Your email address will not be published. Required fields are marked *