Posted by: Tushar | July 9, 2007

ActionScript 3: Creating custom event handlers using EventDispatcher

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.


Responses

  1. Hi, Good one :)

  2. 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.

  3. sara:

    You’re a dumbass.

  4. Grat, it’s very usefull, even more i love as3

  5. changed imageLoader to ImageLoader in all places where it occurred. now works without error.

  6. nice class.

  7. 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.

  8. can anyone load image through urlloader?

  9. I agree with sara actually. Extraneous code, italics and no helpful formatting or higlighting make this very difficult to understand for beginners.

  10. 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

  11. Very Helpful, sorted out alot of problems for me thanks

  12. 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.

  13. thank you verry much was searching long for creating custom events

  14. thanks for your sharing

  15. 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)

  16. James, what in this code is extraneous?

  17. Thanks,it help me to understand custom events,thanks.

  18. 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

  19. 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

  20. 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

  21. 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.

  22. 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.

  23. S, this example is one of the simplest way of having custom Event (i.e. onImageLoad).

    Regards, Tushar


Leave a response

Your response:

Categories