Replacing Flex’s Timer with Event.ENTER_FRAME
One of the projects I’m working on, as you might have guessed, utilizes Adobe’s AIR. After reading a few dozen optimization tips it sounded like I needed to replace four internal Timer‘s because they aren’t very efficient. As I couldn’t find anything off-hand that suited my needs I rolled my own (with some optimizations by Noah Massey).
The original code looked similar to:
private var timer:Timer = new Timer(15000); // run every 15 seconds
private function onCreation(event:Event):void {
this.timer.addEventListener(TimerEvent.TIMER, this.onTimer, false, 0, true);
this.timer.start();
}
private function onTimer(event:TimerEvent):void {
/* do something */
}
I didn’t want to totally refactor all of my code because that would have been an amazing waste of time. So I ran through my usage of “timer” and realized what I needed to be able to do:
- start
- stop
- change interval
- create run-once functions like setTimeout, but tied to frames
With this in mind I typed up what I wanted and after a bit of tweaking my usage now looks like this:
private function onCreation(event:Event):void {
FrameActivity.assign('someName', 15, this.onTimer);
this.addEventListener(Event.ENTER_FRAME, FrameActivity.handleFrame, false, 0, true);
}
private function onTimer(event:Event):void {
/* do something */
}
If you like what you see, send me a comment and I’ll happily provide the code.
Leave a Reply
You must be logged in to post a comment.