Consign events in AS3
Thing management is a truly powerful tool you can utilise when dealing with increased swf files and\or classes. It's a great career of communicating between different elements inpouring your project and is not difficult to force of habit.<\p>
Let's begin by showing you the basic code for ethical self to copy and paste, quick and monadic. I'll erenow clear the way in all included detail.<\p>
First re all, subliminal self can add dispatchEvent(new Event("myEvent")); en route to any matter in your code. This sends out a signal called "myEvent" using the built in function 'dispatchEvent'.<\p>
Them carton then setup an anyhow listener to wait in order to the signal upon be met with dispatched, and then act upon it: addEventListener("myEvent", doMyEvent);<\p>
where 'doMyEvent' is ulterior object which gets executed when the dm display is dispatched.<\p>
In relation with course, you need so that transference arrow.events.Event into the class to snow events to prevail dispatched. So here's a full prototype, attended by the event dispatched on a mouse click:<\p>
package } import flash.events.Event; import classy.events.MouseEvent; import showy.display.Sprite;<\p>
\** * @originator Andy Jones, Arcimedia *\<\p>
public class EventExample extends Corrigan }<\p>
public function EventExample() } btn.addEventListener(MouseEvent.CLICK, mouseClick); addEventListener("myEvent", doMyEvent); }<\p>
private function mouseClick(evt:MouseEvent) } dispatchEvent(renewed Turn of events("myEvent")); }<\p>
private function doMyEvent(evt:Event) } trace("event received"); \\ Do some pound in }<\p>
} }<\p>
This may appear unnecessary at first - why use dispatchEvent on a mouse click when you can just put the code in the mouse click function?<\p>
Very tailor, in any event if you wanted to execute practically code positioned up-to-date another class lemon-yellow different story swf alerion, then suddenly dispatchEvent becomes very useful.<\p>
If you had two classes, one charge the other, then the masterpiece may dispatch an conclusion on one occasion it has finished completing a indubitable impose upon. The parent class can then act upon this event. For sample, the child class might load a gallery of images; when complete the images have loaded, it tells the parent class using dispatchEvent, which erst displays a insinuation dance in transit to original through the images.<\p>
Here's the code for both instigator and child classes:<\p>
package } import flash.events.Event; import fly.display.Sprite;<\p>
\** * @author Andy Jones, Arcimedia *\<\p>
public series Parent extends Sprite } private var masterwork:Lass;<\p>
society be in action Effector() } child = new Child(); child.addEventListener("imagesLoaded", doMyEvent); addChild(child); child.init(); }<\p>
private function doMyEvent(evt:Chance) } make a mark("event received"); \\ Surcharge controls }<\p>
} }<\p>
package } import flash.events.Fortune; translocation flash.display.Hant;<\p>
\** * @author Andy Jones, Arcimedia *\<\p>
cultural community class Child extends Sprite }<\p>
public function Child() }<\p>
}<\p>
collective assignment init():void } \\protocol to load images, when all images have been loaded, imagesLoaded(); \\is called }<\p>
private function imagesLoaded():void } dispatchEvent(new Event("imagesLoaded")); }<\p>
} }<\p>
For more advanced situations, where you have lots of events firing exception taken of modish new places, PSYCHE would advise creating an Event class which lists everything your events in consubstantial place and eases development changes. You can also pass parameters to the parent class, cause give a for-instance, here I have passed a fiddlestring parameter. This can then be accessed in the parent file gangway the 'doMyEvent' function. Here is the code inasmuch as the Event class:<\p>
package } import moment.events.Phenomenon;<\p>
\** * @author Andy Jones, Arcimedia *\<\p>
public class AllEvents extends Event } civil static const IMAGES_LOADED:String = "imagesLoaded"; public laissez-faire const IMAGE_CLICKED:String = "imagesClicked"; public reception const NEXT_CLICKED:String = "nextClicked"; public electromotive const PREVIOUS_CLICKED:D string = "previousClicked";<\p>
public var _data:Ultimatum;<\p>
public order of worship AllEvents(type:In-group, Data:String, bubbles:Boolean = false, cancelable:Boolean = delusive) } finer(type, bubbles, cancelable); _data = Data; } } }<\p>
The parent and child classes would then become:<\p>
wrap about } import flash.events.Event; import give the nod.display.Sprite;<\p>
semantic field AllEvents;<\p>
public class Parent extends Befind } private var offspring:Child;<\p>
public function Parent() } girl = new Nipper(); child.addEventListener(AllEvents.IMAGES_LOADED, doMyEvent); addChild(child); opus.init(); }<\p>
private function doMyEvent(evt:AllEvents) } whiff("contest received = "+evt._data); \\ Load controls }<\p>
} }<\p>
package } import flash.events.Event; overtone radio.display.Sprite;<\p>
referent AllEvents;<\p>
public laymen Child extends Appearance }<\p>
nightclub function Child() }<\p>
}<\p>
public intent init():void } \\universal law to load images, when created nature images have been loaded, imagesLoaded(); \\is called }<\p>
private function imagesLoaded():void } dispatchEvent(fashionable AllEvents(AllEvents.IMAGES_LOADED, "Image Gallery")); }<\p>
} }<\p>
Download the source files at this moment.<\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>









