Creating custom event handlers in ActionScript 3 is pretty much simple. Here is a simple example which loads an image and fires a custom event named “onImageLoad” as soon as image is loaded.
Class imageLoader.as
package {
import flash.events.*;
import flash.net.URLRequest;
import flash.display.Loader;
import flash.display.Sprite;
public class imageLoader extends EventDispatcher {
private var _mc:Sprite;
private var url:String;
private var loader:Loader;
public function imageLoader(_mc:Sprite, url:String) {
this._mc = _mc;
this.url = url;
loadImg();
_mc.addChild(loader);
}
private function loadImg():void {
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
var request:URLRequest = new URLRequest(url);
loader.load(request);
}
private function onComplete(event:Event):void {
dispatchEvent(new Event("onImageLoad"));
}
private function onIOError(event:IOErrorEvent):void {
trace("IO Error.");
}
}
}
Now in Flash, create a a object of imageLoader class and add a event listener (for our custom event “onImageLoad”).
var myImageLoader:imageLoader = new imageLoader(this, "images/someImg.jpg");
myImageLoader.addEventListener("onImageLoad", callbackHandler);
function callbackHandler(event:Event) {
trace("Image Loading Complete!");
}
Now test the file. Be sure to have someImg.jpg in images folder. Once image is loaded, you should be able to see ”"Image Loading Complete!” message in output window.




Hi, Good one
By: Devaraj on July 10, 2007
at 5:40 am
hi tushar,
examples you give are actually very good.but i want to suggest one thing that can you give very simple example avoiding too much coding and with only necessary syntax? it will help people who are new in AS2 or AS3. for you AS2 is old version but many companies have just started using AS2.
By: sara on September 6, 2007
at 11:42 am
sara:
You’re a dumbass.
By: asdlfkj on October 24, 2007
at 7:30 pm
Grat, it’s very usefull, even more i love as3
By: kassel on November 22, 2007
at 6:43 pm
changed imageLoader to ImageLoader in all places where it occurred. now works without error.
By: Kevin on December 22, 2007
at 2:53 am
nice class.
By: noponies on January 3, 2008
at 11:56 pm
how about removing it? i dun seem to get it to remove the “loaded img” and have it ready to load a new image. any idea? thx.
By: chyi on January 29, 2008
at 7:59 pm
can anyone load image through urlloader?
By: rehmat on February 11, 2008
at 8:36 am
I agree with sara actually. Extraneous code, italics and no helpful formatting or higlighting make this very difficult to understand for beginners.
By: James on April 15, 2008
at 11:34 am
Common asdlfkj… Really? Lighten up, dude.
Sarah,
There is not really any code there that doesn’t need to be. AS3 looks a bit more convoluted, but it’s structure is consistent and you get used to it. Once you get past how it “looks” like more code you will learn it’s much nicer than AS2.
Unfortunately most folks are still using the flash 8 player so AS2 is still in demand. But I strongly urge you to start learning AS3 now. Once you wrap your head around it you will see it is much easier to write than AS2.
It was a tough jump for me but I’m much happier now
Check out “Learning ActionScript 3 – A Beginner’s Guide” by Rich Shupe with Zevan Rosser. It puts practical examples into plain terms… very good. Also the more advance reference book “Essential ActionScript 3.0″ by Colin Moock… this book truly is essential after you have decent footing in AS3.
-W
By: will on April 16, 2008
at 7:34 pm
Very Helpful, sorted out alot of problems for me thanks
By: Matt Jukes on August 1, 2008
at 1:05 pm
I just finished a fairly heavy AS3 application, and although I feel I kept a pretty good OOP model I never used custom events. About 80% of the way through I really started to understand why I should of… near the end of the project a lot of nice OOP code started to become pretty procedural and hacky because of certain requests.
By: Ry on August 11, 2008
at 8:27 pm
thank you verry much was searching long for creating custom events
By: SJAAK on October 7, 2008
at 7:58 am
thanks for your sharing
By: wc on February 24, 2009
at 7:52 am
Will, agreed lol
AS3 IS ALLOT different to AS2, for me AS2 is easier to learn then AS3 but I guess I am not sure what language AS3 is akin to (seems like C# but not sure)
By: Max on March 3, 2009
at 2:57 am
James, what in this code is extraneous?
By: Tina on March 10, 2009
at 10:10 pm
Thanks,it help me to understand custom events,thanks.
By: Santu on June 25, 2009
at 6:26 am
Hi Tushar,
I am flash developer with 6 yrs of experience, more into elearning development. I just want to know how good/suitable is flex for elearning development.
Thanks
B.S
By: bshan on July 27, 2009
at 11:58 am
Hi Tushar,
Thanks you for the reply. I have one more question. The Company I m currently working with involved in Game development – online as well as PC games and elearning. My question is, can Flex be used for any of these developments to increase productivity?
B.S.
Hyderabad, India
By: bshan on July 28, 2009
at 10:34 am
Hi Tushar,
Can you give me any example where flex is used for Online Game statistics or is it good to use flex for statics for the game developed in flash?.
Thanks
B.S
By: bshan on August 3, 2009
at 5:03 am
This is by far the most practical example in using custom events. I have been at it for months but was not able to get my head around events and dispatchers. This has really put it together for me. Huge thanks for the post.
By: ThaJock on August 25, 2009
at 11:18 pm
This is not a custom event handler. This is a custom event listener and custom event dispatcher. A custom event handler is quite different and a bit more complicated.
That aside this is a good tutorial.
By: S on August 31, 2009
at 3:36 pm
S, this example is one of the simplest way of having custom Event (i.e. onImageLoad).
Regards, Tushar
By: Tushar on September 1, 2009
at 3:27 am
thank god…the easiest most stripped down explanation I’ve found
By: stowns on January 11, 2010
at 9:37 pm
hi tushar,
examples you give are actually very good. But it is still old.
By: sara on February 4, 2010
at 10:16 am
Thanks, that’s all I needed to help me understand custom event listeners.
By: Matt on April 5, 2010
at 5:06 pm
Thanks
but can i ask you question?
why we are using custom Events ?
and where we can do it?
thanks
By: Arasa on April 24, 2010
at 1:07 pm
how do you index an event so that i would like to load 2 pic that are loaded in a extended sprite.
For example: i’ve loaded one picture(duplicated the image to fil the matrix) basicly onFront(4×4 matrix), and behind them other random pic (one by one) onBehind.
How do i launch an Mouse event on both picture, front and back , one to be visible an the other not, at the same time ?
By: nicholas on May 6, 2010
at 9:36 am
Hi people,
I tried this code but kept receiving the error message “Packages cannot be nested”
I tried it on a clean new page, trying to load a .swf file on it.
I ave been doing this with AS2 but cannot really get on to it with AS3…d
Can anypne help me?
Thaks a lot in advance
By: No-mad on January 10, 2011
at 9:07 pm
Tushar Wadekar,
I want to marry you, you are a genius, I am having problem with loading swf file and I was running conversion from AS2 to AS3 and your code works for “someSWF.swf” movie file as well, Thanks,
By: Rasmi on May 12, 2011
at 1:11 am
I’ve already done this couple times, but now I’m in a trouble cause of listeners not being reached by the dispatched events.
I have a class that wraps couple constants used dispatching custom events, let’s say “MyEvent”. Then I have a class (in the same package) that dispatch those messages, let’s say “MyEngine”, and another class where I declare an object of the MyEngine class, let’s say “MyMainClass”.
I add an eventListener to the MyMainClass object, then I declare an object of MyEngine class that dispatches couple events. The point is that the eventListeners are not triggered! They are correctly triggered if the callback function in put in the MyEngine class as well as the eventListener, too.
It looks like the event wasn’t “forwarded” to the MyMainClass…
Any suggestions?
P.S. I used to do the same in the same project while it was simpler and I got that 100% working. Now that its dimension is grown, I get this weird behaviour
Thank you in advance for your help.
By: Jack on August 27, 2011
at 3:48 pm
[...] you are using AS3, maybe you can refer below post: http://tush.wordpress.com/2007/07/09/actionscript-3-creating-custom-ev ent-handlers-using-eventdispa… Related [...]
By: Problem writing event » free icons download on April 30, 2012
at 2:53 am
Excellent website. Plenty of useful information here. I am sending it to several buddies
ans additionally sharing in delicious. And naturally, thanks in your effort!
By: 2013 if design awards on March 22, 2013
at 2:08 pm