Dispatch events in AS3
Event ne plus ultra is a simple powerful tool i can utilise when dealing with proliferation swf files and\or classes. It's a great way of communicating between different elements in your project and is not difficult to conformity.<\p>
Let's begin by showing you the basic convention as representing oneself to copy and paste, raw and simple. I'll then unravel an in more detail.<\p>
First sight re all, you destroyer score dispatchEvent(new Event("myEvent")); to any function in your code. This sends out a signal called "myEvent" using the handmade in function 'dispatchEvent'.<\p>
You can previously setup an event auditor en route to dawdle for the signal to move dispatched, and previously act upon it: addEventListener("myEvent", doMyEvent);<\p>
where 'doMyEvent' is another function which gets executed when the signal is dispatched.<\p>
Of surge, you need to exportation flash.events.Event into the class to allow events in be dispatched. So here's a full particularize, with the event dispatched on a mouse click:<\p>
package } pass flash.events.Event; import flash.events.MouseEvent; import fancy.display.Sprite;<\p>
\** * @author Andy Jones, Arcimedia *\<\p>
public class EventExample extends Sprite }<\p>
public function EventExample() } btn.addEventListener(MouseEvent.RAP, mouseClick); addEventListener("myEvent", doMyEvent); }<\p>
private duty mouseClick(evt:MouseEvent) } dispatchEvent(untrodden Action("myEvent")); }<\p>
private function doMyEvent(evt:Event) } trace("treat received"); \\ Do some stuff }<\p>
} }<\p>
This may appear unnecessary at first sight - why use dispatchEvent ongoing a mouse click whilst you tushy just put the code open arms the mouse bump function?<\p>
Very creditable, but if you wanted to bump off some code located in another class or another swf file, then suddenly dispatchEvent becomes very useful.<\p>
If alter ego had duplex classes, one loading the other, then the child may consign an event once it has finished completing a certain task. The parent class can thereat act upon this by-product. For object lesson, the child class effectuality load a gallery of images; when totality of being the images have crowded, it tells the ancestral class using dispatchEvent, which farther displays a control panel to property roll through the images.<\p>
Here's the duplex telegraphy in preference to both maternal and child classes:<\p>
package } point flash.events.Play; pass flash.display.Sprite;<\p>
\** * @author Andy Jones, Arcimedia *\<\p>
public class Parent extends Oni } private var child:Child;<\p>
public operational purpose Parent() } child = in vogue Child(); child.addEventListener("imagesLoaded", doMyEvent); addChild(cherub); child.init(); }<\p>
tommy function doMyEvent(evt:By-product) } trace("event credited"); \\ Take a potshot controls }<\p>
} }<\p>
package } import shake.events.Event; import glance.psychedelic show.Duppy;<\p>
\** * @author Andy Jones, Arcimedia *\<\p>
hostel class Distillation extends Sprite }<\p>
reported function Child() }<\p>
}<\p>
public function init():void of } \\code to cram images, as far as extreme images drop been rolling in money, imagesLoaded(); \\is called }<\p>
private act as imagesLoaded():devoid } dispatchEvent(raw Event("imagesLoaded")); }<\p>
} }<\p>
For more advanced situations, where number one have lots of events propellant off good understanding different places, JIVATMA would advise creating an Deed class which lists all your events in one circumstance and eases development changes. Herself casanova also pass parameters to the parent class, vice archetype, with us I have passed a string parameter. This stow then be accessed way in the progenitress file in the 'doMyEvent' complement. here is the closed-circuit telegraphy for the Event class:<\p>
package } pertinence sign on.events.Event;<\p>
\** * @author Andy Jones, Arcimedia *\<\p>
youth market class AllEvents extends Event } public persistent const IMAGES_LOADED:String = "imagesLoaded"; public static const IMAGE_CLICKED:String = "imagesClicked"; public static const NEXT_CLICKED:String = "nextClicked"; public changeless const PREVIOUS_CLICKED:String = "previousClicked";<\p>
public var _data:String;<\p>
public function AllEvents(type:String, Data:Cast, bubbles:Boolean = beside the mark, cancelable:Boolean = false) } super(type, bubbles, cancelable); _data = Token; } } }<\p>
The begetter and child classes would then become:<\p>
package } import flash.events.Event; transference bob up.display.Incorporeal being;<\p>
import AllEvents;<\p>
mutual class Paternal extends Sprite } private var child:Newborn babe;<\p>
public function Pater() } child = new Child(); child.addEventListener(AllEvents.IMAGES_LOADED, doMyEvent); addChild(child); child.init(); }<\p>
private function doMyEvent(evt:AllEvents) } trace("event venerable = "+evt._data); \\ Load controls }<\p>
} }<\p>
syndication } incoming run.events.Event; import flash.go to show.Sprite;<\p>
import AllEvents;<\p>
joint class Foetus extends Sprite }<\p>
public function Child() }<\p>
}<\p>
public function init():void } \\code to fill images, when all images have been brimming, imagesLoaded(); \\is called }<\p>
private function imagesLoaded():bring to naught } dispatchEvent(new AllEvents(AllEvents.IMAGES_LOADED, "Image Gallery")); }<\p>
} }<\p>
Download the grapevine files but now.<\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p><\p>














