Creating custom events in ActionScript 3.0 is simple. Use them, then, allows you to use the method addEventListener() and make our object of all standards. ) BottoneEvent . If we created a class, such as Bottone , this is correct to associate with one or more events through a special class (derived from the class Event ) BottoneEvent . We could make all the events that affect the operation of our subject: the click, mouse over, or any other event! As well as defining our personal basis. The skeleton - generic - a custom event class is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package { / * ** @ Name: BottoneEvent.as ** @ Description: Class derived from Event to manage their own custom event ** @ Author: undo = = ** @ Web: http://www.undolog.com ** @ Email: @ g.fazioli undolog.com * / .*; import flash.events .*; BottoneEvent extends Event { BottoneEvent extends public class Event { String = 'pluto' ; // codice qualsiasi, anche 'mioevento' in minuscolo public static const MIOEVENTO: String = 'bar', / / any code, even 'mioevento' in lower case Valore : Number = NaN ; public var value: Number = NaN; / * ** @ Name: BottoneEvent ** @ Description: constructor * / type , bubbles , cancelable ) ; super (type, bubbles, cancelable); Value = v ; this. Value = v; } / * ** @ Override * / clone ( ) : Event { override public function clone (): Event { this . type , this . Value , this . bubbles , this . cancelable ) ; KnobEvent return new (this. type, this. Value, this. bubbles, this. cancelable); } } } |
In the class Bottone , the situation is even easier! Take for example the portion of code that generates the event, within the class Bottone :
1 2 3 4 5 6 7 8 | / * ** @ Name: myMethod () * / mioMetodo ( ) : void { public function myMethod (): void { / / Something happens and we need to create an event / / Passing a value new BottoneEvent ( BottoneEvent . MIOEVENTO , 1234 ) ) ; dispatchEvent (new BottoneEvent (BottoneEvent. MIOEVENTO, 1234)); } |
The application side, of course, we will use our class Bottone like all the others:
1 2 3 4 5 6 7 8 9 10 | Import button; BottoneEvent imports; / / Bottone = new Bottone ( ) ; var foo: button = new Button (); ( BottoneEvent . MIOEVENTO , foo. addEventListener (BottoneEvent. MIOEVENTO, e : BottoneEvent ) : void { function (e: BottoneEvent): void { / / Do! e . type + ' / ' + e . Value ) ; // 'pluto / 1234' trace (and. type + '/' + and. Value) / / 'bar / 1234' } ); |










There are no comments for this post
Leave a comment