View Full Version : Event propagation between classes? [AS3]
Valaran
July 17th, 2008, 03:44 PM
Hello,
I've been reading up on Event's in AS3 today, and... I don't grasp it completely... Actually I fail pretty bad :crying:. Anyway, is it possible to accomplish event propagation between classes? If it is, say I have a Class called CustomClass and it creates a instance of another Class, CustomSubClass in it, would propagation be possible in such a manner? And if it is, would those custom classes need to extend EventDispatcher or similar?
Thanks for any possible clarification,
Matt
senocular
July 17th, 2008, 04:07 PM
propagation only occurs through display objects. Events propagate (if dispatched with propagation) through the display hierarchy allowing them to reach the different display objects that represent the object in which the event technically occurred. When you create non-display objects composed within each other, there is no path for an event to take to reach each of those objects. The event will only occur in the object dispatching it. From there you can have other objects listen to that same object, or have an object listen to the source object and dispatch another event for other objects to listen to it instead of the source object.
For example, given instances of classes A, B, and C where A contains a B and B contains a C; if C dispatches an event that both A and B need to react to, you can 1) have both A and B explicitly target C and add listeners to the C object, or 2) have B listen to C, and A listen to B. When B receives the event in its event handler, dispatch a new event (or the same event) so that A will also receive it as a result of listening to B. 1) is a little more efficient since you're not re-dispatching events, but 2) does a better job enforcing encapsulation.
sekasi
July 17th, 2008, 04:17 PM
Alternatively you can have A listen to Class D that is a singelton and handles all events from B and C that are targeted to A :)
senocular
July 17th, 2008, 04:24 PM
Does it have to be a singleton :!:
sekasi
July 17th, 2008, 04:25 PM
Otherwise they would be listening/dispatching to different instances ?
Valaran
July 17th, 2008, 04:28 PM
That clarified some, thanks Senocular, and Sekasi, could you please explain the term singleton to me? :) Oh! And another question, to be able to add a listener to "randomCustomClass", that class would have to extend EventDispatcher or another class that extends EventDispatcher, right?
Thanks again,
Matt
senocular
July 17th, 2008, 04:34 PM
That clarified some, thanks Senocular, and Sekasi, could you please explain the term singleton to me? :)
Singletons are classes where only one can exist in an application. They can be easy to use in some cases, but can cause problems when it comes to scalability. Some people think they're the devil as well (I know Luke Bayes thinks so ;) )
You usually see them as:
myInstance = YourSingletonClass.getInstance();
where myInstance is the instance of the singleton. You can use that anywhere and always have the same instance and there's no way to get another (using new YourSingletonClass is not allowed).
Oh! And another question, to be able to add a listener to "randomCustomClass", that class would have to extend EventDispatcher or another class that extends EventDispatcher, right?
For the most part. However, you don't HAVE to. Instead you can use composition to keep an instance of an EventDispatcher in your non-extending-EventDispatcher class and have it dispatch events on behalf of your class for you. Check out the AS3 tip of the day thread. I'm pretty sure I put an example there.
Valaran
July 17th, 2008, 04:41 PM
Will do, thanks again.
senocular
July 17th, 2008, 04:43 PM
Here is that event dispatcher post I was talking about:
http://www.kirupa.com/forum/showthread.php?p=1899603
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.