Lol, you can write whole books on Events and Event Propagation, but I'll try to be concise.
Event propagation is all about letting one part of your application know that another part of the application is carrying out (or has carried out) a specific task.
For instance, an URLLoader can 'dispatch' an event stating that it has finished loading an XML file. And you can act on those events by adding an event listener to, for instance, this URLLoader.
But the real power of event dispatching is that you can create your own, custom events. You can either create your own class, which extends the Event class. Or you can just use the Event class to send an event.
Dispatching events is done like this:
dispatchEvent(new Event(Event.COMPLETE)); // dispatching a predefined flash event
dispatchEvent(new Event("myEvent")); // dispatching a custom event via AS's Event class
dispatchEvent(new CustomEvent(CustomEvent.COMPLETE)); // dispatching a custom event via your own custom event class
To listen to these events, you would use the following code:
addEventListener(Event.COMPLETE, myEventHandler);
addEventListener("myEvent", myEventHandler);
addEventListener(CustomEvent.COMPLETE, myEventHandler);
Now, by default, event -bubbling- is set to false. This means that an event is only available in the class that dispatched the event.
So imagine the following situation, you have your main application class, and in that class, you create an instance of a Ball class and you add that ball to the stage. In the Ball class, you write some code that dispatches an event everytime the ball bounces:
Ball.as:
private function bounce():void {
// some code to let my ball bounce
dispatchEvent(new CustomEvent(CustomEvent.BOUNCE));
}
If we want to catch that in the main class, we would have to do the following:
Main.as:
var ball:Ball = new Ball();
ball.addEventListener(CustomEvent.BOUNCE, myBounceHandler);
addChild(ball);
This is a good method if the dispatcher can be accessed from the class where you put the listener. (ball was declared in the main class, and we could easily attach a listener to it) But imagine now that you have a ball bath. A container filled with bouncy balls:
Main.as:
var container:BallBath = new BallBath();
addChild(container);
You have no access to the ball instances, so you cannot attach a listener to them. This is where event propagation and the event model comes into play. I've talked about bubbling before, and how it is set to false by default. Well lets set it to true now:
Ball.as:
dispatchEvent(new CustomEvent(CustomEvent.BOUNCE, true));
See how I added the boolean behind the event type? This means that event bubbling is now active. When event bubbling is active, an event will travel through the display list, down to your main application. This is called event propagation.
So starting in the Ball class, it passes the event on to the BallBath class and from there, again passes it on to the Main class. (
bubbling only works with Display Objects that were added to the stage using addChild.)
Once event bubbling is active, we can listen to our event like so:
Main.as:
this.addEventListener(CustomEvent.BOUNCE, myBounceHandler);
Notice how I added the event listener to the Main class itself, not to an instance of the Ball class.
Hope this answers some of your questions.
Info on the Event Flow
More info on
Event Handling and specifically,
Event Propagation
Kind regards,
Pieter
General notice: from now on, I would like to ask everyone to put [AS2] or [AS3] (corresponding to the version of actionscript you are using on your project) in front of their post titles when the question is actionscript related! Please help us help you faster. Thank you.