One of the most popular Event being used in AS3 is the EnterFrame Event. Working with Enterframe has some advantages as more than often we will need to update things each frame, or sometimes, defer validation for performance boost. As you all may already know, we can only has EnterFrame listens to DisplayObjects, like Shape, creating an empty shape for every class just for EnterFrame listening is not very efficient, neither in term of memory nor processor cost (remember that listens to EnterFrame event dispatching from multiple objects will slower than listen multiple times to one object).

So, in brief, this small utils will help you a little bit for better working with EnterFrame event in AS3. Not covering performance gains, it shortens your code and support custom parameters + once listeners

Simple usage :

  • Defer doing something – use onNext :
    function doSomething(): void {
    //do something here ! this function will be call only once
  • Keep things updated for every frame and pass some parameters by – use onEach :
    EnterFrame.onEach(doSomeOtherthing, [1, "hello !"]);
    function doSomeOtherthing(param1: int, param2: String): void {
    //do other things here ! this function will be call every frame
    //param1 will has value 1
    //param2 will have value "hello !"

And the source, for guys who like to read how things implemented right away, like me 😀

	import flash.display.Shape;
	import flash.events.Event;
	import flash.utils.Dictionary;
	 * Global EnterFrame provider for better performance and short hand
	 * @author	thienhaflash (thienhaflash@gmail.com)
	 * @version 0.1.0
	 * @updated 21 March 2011
	 * @features support once listeners and each frame callbacks with parameters
	public class EnterFrame
		public static var heart 	: Shape			= new Shape();//listen to EnterFrame Event
		private static var nextList : Dictionary	= new Dictionary();
		private static var eachList	: Dictionary	= new Dictionary();
		private static var eachCnt	: int;

		public static function onNext(f: Function, params : Array = null): void {
			heart.addEventListener(Event.ENTER_FRAME, _onNextFrame);
			nextList[f] = params;

		public static function onEach(f: Function, params: Array = null): void {
			if (eachCnt == 0) heart.addEventListener(Event.ENTER_FRAME, _onEachFrame);
			eachList[f] = params;

		public static function remove_onEach(f: Function): void {
			delete eachList[f]

		private static function _onEachFrame(e:Event):void
			eachCnt = 0;
			for (var f: * in eachList) {
				(f as Function).apply(null, eachList[f]);
			if (eachCnt == 0) heart.removeEventListener(Event.ENTER_FRAME, _onEachFrame); //remove event if there are no items listened

		private static function _onNextFrame(e: Event): void {
			heart.removeEventListener(Event.ENTER_FRAME, _onNextFrame);
			for (var f: * in nextList) {
				(f as Function).apply(null, nextList[f]);
				delete nextList[f];


As you guys may notice, there are currently no listening priority support for this utility but it can easily be added in, do it yourself it you need that, i’m not in to priority that much, especially for EnterFrame. Just feel free to modify and use it the way you want, maybe supports both EnterFrame and ExitFrame 🙂 who knows 🙂

Happy coding, folks ! All comments are more than welcome !


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s