Understanding AS3 custom events
- June 3, 2009 by Emanuele Feronato
- Filed under Actionscript 3, Flash | 12 Comments
Every AS3 programmer uses event listeners to allow objects to become active and listen for specific instructions, such as a mouse click or the beginning of a new frame.
Now it’s time to see how can you create new events, but before entering into this script, let me say it’s not a “do it or die” feature.
You can always perform some if... then... else and achieve the same result, but from a PROgrammer point of view, a code with listeners is more readable than a complex list of conditions to check for events.
In this script, we are counting the time passed like in Understanding AS3 Timer Class, but we want to create a custom event to be triggered every 5 seconds.
Obviously in this case it’s quite pointless to create a custom event, but I am showing you the way you can do it.
This is the script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package { import flash.display.Sprite; import flash.events.Event; import flash.events.TimerEvent; import flash.utils.Timer; import flash.text.TextField; public class dispatch extends Sprite { var dispatcher:time_dispatcher = new time_dispatcher(); var time_count:Timer=new Timer(1000); var interval_timer:TextField = new TextField(); public function dispatch() { interval_timer.x=5; interval_timer.y=5; addChild(interval_timer); time_count.addEventListener(TimerEvent.TIMER,show_time); dispatcher.addEventListener(time_dispatcher.ON_DIV_BY_FIVE, on_event_triggered); time_count.start(); } private function on_event_triggered(event:Event):void { interval_timer.text="TRIGGERED"; } function show_time(event:TimerEvent) { interval_timer.text=event.target.currentCount; dispatcher.check_division(event.target.currentCount); } } } import flash.events.EventDispatcher; import flash.events.Event; class time_dispatcher extends EventDispatcher { public static var ON_DIV_BY_FIVE:String="can be divided"; public function check_division(num):void { if (num%5==0) { dispatchEvent(new Event(time_dispatcher.ON_DIV_BY_FIVE)); } } } |
Line 8: Creating a new dispatcher, aggregating an instance of time_dispatcher class. I included the class in the same file to make the tutorial easier to understand, but you can obviously have your new class in another .as file as usual.
Line 16: This is where add the listener… as you can see it’s the same syntax you are used… except AS3 doesn’t have any ON_DIV_BY_FIVE listener.
The remaining lines are quite the same you’ve seen a thousand times, excluded line 24 where you can for check_division function that will trigger the event if the number of elapsed seconds can be divided by 5.
Line 35: This is how we fire, or dispatch, an event.
As I said, you can always use your if... then statement but in my opinion a code with listeners such as ON_LEVEL_COMPLETED is more readable than something like if(collected_items==total_items && time_left>0){...
This is the result of the script:
Download the source code, and next time I’ll show you how to develop a classic game with listeners.
They can be easily customized to meet the unique requirements of your project.
12 Responses
Leave a Reply
TUTORIAL SERIES:
- Una guida completa al gioco del poker online e una selezione dei migliori casino online.
- casino online
- migliori casino online
- BlackJack online
- casinò online

(30 votes, average: 4.20 out of 5)

Hey Emanuele, thanks a lot for your 3 years devoted to this blog. I come here daily and really appreciate your effort to post almost every day.
It’s really nice to have people like you sharing their knowledge in the flash community.
cheers!
[...] Click here to view the tutorial [...]
Eventually, you have to write your own event classes, which can be really helpful (so instead of a complicated series of checks to find an object in a list, you can just pass a reference in your event!)
I have no idea why people dislike this post so much, but i liked it, 5/5
Thanks for this, the other tutorials out there are really “meh’”
I’ve been experimenting with custom events but this really helped solidify it for me.
Emanuele,
I’ve got a great CustomEvent class that I’ve grown into a great solution for custom event types like this. It also allows for the addition of parameter Objects to be passed with the events which is quite handy in a lot of situations.
I’ll see if I can package it up into a blog post for you and shoot you a link.
Cheers,
-Scott
emanuele please!!!
I need a rope tutorial in Box2D…
you kwow where find it???
I love you
[...] Link to the original ‘Understanding AS3 custom events : Emanuele Feronato’ [...]
I do not want to extend sprite or UIComponent for my class.
I want to write a class which dispatches an event.
Is It possible?
[...] are a few tutorials out there if you are interested in understanding a bit more of how the flash.events.Event class is [...]
[...] [...]
Hello ! Thanks for your useful posts, your website already helped me a couple of times !