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.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (29 votes, average: 4.17 out of 5)
Loading ... Loading ...
If you found this post useful, please consider a small donation.
» Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.

11 Responses to “Understanding AS3 custom events”

  1. Pedro on June 4th, 2009 12:19 am

    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!

  2. HiddenSpartan on June 4th, 2009 3:43 am

    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!)

  3. Alan on June 4th, 2009 7:48 am

    I have no idea why people dislike this post so much, but i liked it, 5/5

  4. Justin Edmond on June 4th, 2009 8:00 pm

    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.

  5. Scott on June 6th, 2009 10:19 pm

    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

  6. zap on June 9th, 2009 9:19 am

    emanuele please!!!

    I need a rope tutorial in Box2D…

    you kwow where find it???

    I love you

  7. Madhu on June 16th, 2009 3:20 pm

    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?

Leave a Reply




Trackbacks

  1. Understanding ActionScript 3.0 custom events | Flash Framer on June 4th, 2009 3:17 am

    [...] Click here to view the tutorial [...]

  2. 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’ [...]

  3. 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 [...]

  4. Variable bei Funktionsaufruf mittels Click ?bergeben - Flashforum on January 15th, 2010 1:56 pm

    [...] [...]

flash games company