I made a Timer class that I use for another set of Classes I wrote. Here
is the class itself, it's pretty precise and reliable so far. Hope it
Helps !
usage :
import ca.daroost.utils.Timer;
soundTimer = new Timer();
soundTimer.addEventListener("onTimerDone", this);
soundTimer.addEventListener("onTimerStart", this);
soundTimer.startTimer(0, 2000); // 2 second timer
function onTimerDone(evtObj:Object) {
trace("Timer is done after "+evtObj.delta+"ms - wanted time is :
"+evtObj.timerDelta+"ms");
}
function onTimerStart(evtObj:Object) {
trace("Timer has started");
}
---- Class Start ---
import mx.events.EventDispatcher;
import mx.utils.Delegate;
class ca.daroost.utils.Timer {
// Constants:
public static var CLASS_REF = ca.daroost.utils.Timer;
// Public Properties:
public var dispatchEvent:Function;
public var addEventListener:Function;
public var removeEventListener:Function;
// Private Properties:
private var tEnd:Number;
private var tStart:Number; // usually this is 0, but in case of a
sound loop there could be a start offset
private var _starttime:Number; // actual timer start value set with
getTimer()
private var tDelta:Number; // Delta time between tEnd and tStart
private var tInterval:Number;
// Initialization:
public function Timer(startTime:Number,endTime:Number) {
EventDispatcher.initialize(this);
tStart = (startTime != undefined) ? startTime : 0;
tEnd = (endTime != undefined) ? endTime : 0;
tDelta = 0;
tInterval = 0;
}
// Public Methods:
public function startTimer(startTime:Number,endTime:Number) {
tEnd = endTime;
tStart = startTime;
tDelta = tEnd - tStart;
_starttime = getTimer();
tInterval = setInterval(this, "updateTimer", 1);
this.dispatchEvent({target:this, type:"onTimerStart",
startTime:tStart, endTime:tEnd});
}
public function set endTime(value:Number):Void {
tEnd = value;
}
public function get endTime():Number {
return tEnd;
}
public function set startTime(value:Number):Void {
tStart = value;
}
public function get startTime():Number {
return tStart;
}
// Semi-Private Methods:
// Private Methods:
private function updateTimer():Void {
var _delta:Number;
_delta = getTimer() - _starttime;
if (_delta >= tDelta -50) {
// here we will get a more precise value with the last 50 ms
while (_delta < tDelta) {
_delta = getTimer() - _starttime;
}
this.dispatchEvent({target:this, type:"onTimerDone",
delta:_delta, timerDelta:tDelta});
clearInterval(tInterval);
}
}
}
---- Class End---
Gustavo Duenas wrote:
Hi, I'm trying to set a timer to trigger a event after 20 seconds or
less, but so far i have this code, but with no results
someone help me? .
I don't know how could I got into but some help might be needed
this is the code: is inside the first frame of a movie clip and the 4
frame has: gotoAndPlay(1);
var now = getTimer();
var seconds:Number = now/1000;
var later:Number = seconds+20;
this.onEnterFrame= function (){
if(seconds == later){
trace("llegamos");
}
}
trace(seconds);
Regards
Gustavo Duenas
_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com
_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com