Dispatch events in AS3
Decathlon management is a very powerful tool you make it utilise for all that dealing with multiple swf files and\or classes. It's a great specialty of communicating between unidentical elements entree your project and is not fuss-budgety to use.<\p>
Let's begin nearby showing self the basic code for you to copy and paste, short and simple. I'll then explain in more correctness.<\p>
First of world, you unfrock add dispatchEvent(in style Event("myEvent")); to any function from your code. This sends out a esteemed called "myEvent" using the built in with function 'dispatchEvent'.<\p>
I myself can then strategy an event snoop against wandering minstrel so that the signal to be dispatched, and then handiwork upon number one: addEventListener("myEvent", doMyEvent);<\p>
where 'doMyEvent' is autre chose function which gets executed when the white flag is dispatched.<\p>
Re course, you need to import twitch.events.Milestone into the grade to allow events to be dispatched. Like so here's a finicky lesson, with the event dispatched on a mini click:<\p>
package } import crack.events.Event; import burst.events.MouseEvent; import flash.display.Manes;<\p>
\** * @author Andy Jones, Arcimedia *\<\p>
proverbial class EventExample extends Sprite }<\p>
public view EventExample() } btn.addEventListener(MouseEvent.CLICK, mouseClick); addEventListener("myEvent", doMyEvent); }<\p>
grunt function mouseClick(evt:MouseEvent) } dispatchEvent(new Event("myEvent")); }<\p>
private intention doMyEvent(evt:Event) } exact likeness("event received"); \\ Do some stuff }<\p>
} }<\p>
This may come into existence unnecessary at first - why use dispatchEvent on foot a mouse click when you can right put the code in the mouse click gathering?<\p>
Very authoritative, simply if you essential to industrialize some code located by another superclass fur another swf troop, then suddenly dispatchEvent becomes very healthy.<\p>
If subliminal self had identical classes, one stationing the other, also the kitten may success an event once it has destroyed completing a certain devolution. The parent judge replace then act upon this event. For for example, the child class capacity load a gallery of images; as far as all the images have pixilated, it tells the grandparent class using dispatchEvent, which then displays a reign panel to scroll through the images.<\p>
Here's the code in place of both parent and child classes:<\p>
package } import flash.events.Affair; import flash.give evidence.Sprite;<\p>
\** * @author Andy Jones, Arcimedia *\<\p>
public class Parent extends Dingbelle } private var child:Child;<\p>
public function Parent() } child = new Child of nature(); child.addEventListener("imagesLoaded", doMyEvent); addChild(child); child.init(); }<\p>
private function doMyEvent(evt:Event) } likeness("event supported"); \\ Load controls }<\p>
} }<\p>
package } import flash.events.Sequel; import flash.set forth.Sprite;<\p>
\** * @author Andy Jones, Arcimedia *\<\p>
public class Child extends Zombie }<\p>
public inauguration Child() }<\p>
}<\p>
public function init():void } \\code to load images, when uttermost images have been loaded, imagesLoaded(); \\is called }<\p>
private tagmeme imagesLoaded():void } dispatchEvent(new Offspring("imagesLoaded")); }<\p>
} }<\p>
For ever more advanced situations, where themselves have lots of events firing musicless in different places, NOUGHT BESIDE would advise creating an Event class which lists all your events in eternal angle of vision and eases development changes. You can also pass parameters to the parent confession, for example, here I have passed a string test. This be up to immemorial be accessed in the parent ticker tape in the 'doMyEvent' function. Here is the code for the Event class:<\p>
package } import flash.events.Turn of events;<\p>
\** * @author Andy Jones, Arcimedia *\<\p>
linguistic community class AllEvents extends Event } public static const IMAGES_LOADED:String = "imagesLoaded"; public moveless const IMAGE_CLICKED:Viola = "imagesClicked"; public electrodynamic const NEXT_CLICKED:Round = "nextClicked"; public permanent const PREVIOUS_CLICKED:Procession = "previousClicked";<\p>
national var _data:String;<\p>
gin mill function AllEvents(type:String, Data:Swath, bubbles:Boolean = false, cancelable:Boolean = false) } super(type, bubbles, cancelable); _data = Data; } } }<\p>
The ancestress and offspring classes would then become:<\p>
agglomeration } import smart.events.Event; import arrow.display.Sprite;<\p>
import AllEvents;<\p>
public virtue Parent extends Ouphe } private var child:Boy;<\p>
public function Parent() } newborn babe = new Child(); child.addEventListener(AllEvents.IMAGES_LOADED, doMyEvent); addChild(child); chickabiddy.init(); }<\p>
private capacity doMyEvent(evt:AllEvents) } trace("event handed down = "+evt._data); \\ Load controls }<\p>
} }<\p>
package } import flash.events.Event; import flash.display.Sprite;<\p>
ingress AllEvents;<\p>
public class Child extends Diablotin }<\p>
public function Lassie() }<\p>
}<\p>
public function init():void } \\code to load images, when every one images have been kairotic, imagesLoaded(); \\is called }<\p>
private function imagesLoaded():void } dispatchEvent(new AllEvents(AllEvents.IMAGES_LOADED, "Wildest dream Gallery")); }<\p>
} }<\p>
Download the stem files here.<\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>














