Understanding AS3 custom events
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.
11 Responses to “Understanding AS3 custom events”
Leave a Reply
Trackbacks
-
Understanding ActionScript 3.0 custom events | Flash Framer on
June 4th, 2009 3:17 am
[...] Click here to view the tutorial [...]
-
Understanding AS3 custom events : Emanuele Feronato | Evan Mullins Circlecube ReBlog on
June 12th, 2009 7:11 pm
[...] Link to the original ‘Understanding AS3 custom events : Emanuele Feronato’ [...]
-
Simple Progressive Download Flash Slideshow – Part 1 « Fusing the Web on
September 27th, 2009 10:29 pm
[...] are a few tutorials out there if you are interested in understanding a bit more of how the flash.events.Event class is [...]
-
Variable bei Funktionsaufruf mittels Click ?bergeben - Flashforum on
January 15th, 2010 1:56 pm
[...] [...]
- Citrus Engine released for free for learning
- My epic fail with ClickBank
- Get up to $100,000 for your next Flash game with Mochi GAME Developer Fund
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines – 17 lines version
- Sell sitelocked version of your Flash games and even .fla sources to Free Online Games
- Protect your work from ActionScript code theft with SWF Protector
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines
- Understanding Box2D’s one-way platforms, aka CLOUDS
- Triqui MochiAds Arcade plugin for WordPress upgraded to 1.2
- Box2D Flash game creation tutorial – part 2
- Create a Lightbox effect only with CSS - no javascript needed
- Flash game creation tutorial - part 1
- Create a Flash Racing Game Tutorial
- Flash game creation tutorial - part 2
- Make a Flash game like Flash Element Tower Defense - Part 2
- Flash game creation tutorial - part 3
- Triqui MochiAds Arcade plugin for WordPress official page
- Make a Flash game like Flash Element Tower Defense - Part 1
- Create a flash draw game like Line Rider or others - part 1
- Create a flash artillery game - step 1
- Flash game creation tutorial – part 5.2 (4.88/5)
- Create a flash artillery game – step 1 (4.79/5)
- Create a Flash Racing Game Tutorial (4.76/5)
- Create a survival horror game in Flash tutorial – part 1 (4.74/5)
- Create a flash artillery game – step 2 (4.74/5)
- Creation of a Flash arcade site using WordPress – step 2 (4.73/5)
- Flash game creation tutorial – part 1 (4.71/5)
- Flash game creation tutorial – part 2 (4.71/5)
- Create a flash draw game like Line Rider or others – part 1 (4.69/5)
- Creation of a platform game with Flash – step 2 (4.68/5)

(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!
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
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?