flash personalised_events_extending
Extending the EventDispatcher
Remember that all the classes residing in the flash package are inherited from the EventDispatcher class. In our projects certain classes don’t inherit native classes and don’t have the possibility to broadcast events by default.
Let’s take the case of a class called XMLLoader having to load the XML data. We want to indicate the end of the loading of data by broadcasting an appropriate event.
The following code illustrates the content of the class:
package { import flash.events.Event; public class XMLLoader { public function XMLLoader () { // code not indicated } public function charge ( pXML:String ):void { // code not indicated } public function loadingFinished ( pEvt:Event ):void { dispatchEvent ( new Event ( XMLLoader.COMPLETE ) ); } } }
If we try to compile this class, an error message warns us that no dispatchEvent method exists in the class. In order to obtain the broadcast capacities of the XMLLoader class inherited from the EventDispatcher class:
package { import flash.events.Event; import flash.events.EventDispatcher; public class XMLLoader extends EventDispatcher { public static const COMPLETE:String = "complete"; public function XMLLoader () { // code not indicated } public function charge ( pXML:String ):void { // code not indicated } public function loadingFinished ( pEvt:Event ):void { dispatchEvent ( new Event ( XMLLoader.COMPLETE ) ); } } }
By sub classing the EventDispatcher, the XMLLoader class can broadcast events. In order to listen to the XMLLoader.COMPLETE event, we write the following code:
// creation of an instance of the XMLLoader var XMLLoader:XMLLoader = new XMLLoader(); // loading of data XMLLoader.charge ("news.xml"); // listening to the XMLLoader.COMPLETE event XMLLoader.addEventListener ( XMLLoader.COMPLETE, loadingFinished ); // listener function function loadingFinished ( pEvt:Event ):void { trace( "loaded data"); }
When we call the load method, the XML data is loaded. Once the loading has finished we broadcast the XMLLoader.COMPLETE event.
One part of the code of the XMLLoader class is not shown as we haven’t yet looked at the idea of external loading. We will come back to this class in the chapter entitled Loading and sending of data in order to complete it.
Even though this technique is efficient, it is not the most optimized. As we saw in the chapter entitled Object oriented programming, ActionScript doesn’t have multiple inheritance. By inheriting the EventDispatcher class the XMLLoader class cannot inherit another class. We would break the inheritance chain. And even worse, if the XMLLoader class had to extend another class we would extend the EventDispatcher at the same time.
In a scenario such as this we would use an essential concept of object oriented programming discussed in the previous chapter. Onwards to see what it is…
Mediabox Training Centre © 2000 - 2008 All rights reserved.
Adobe Authorized Training Centre. State convention under number 25 14 02167 14.
Mediabox : SARL au capital de 62.000€ - Activity number: 25 14 02167 14 - SIRET : 493 716 468 00027
MEDIABOX, 102 Avenue des Champs Elysées, 75008 PARIS - Tel. +33(0)2.31.91.96.89 - Fax. +33(0)2.72.68.56.42


