Creating custom events in ActionScript 3.0
Sunday, May 18, 2008 Creating custom events in ActionScript 3.0 is simple. Use them, then lets you use the method addEventListener() and make our object at all standardized. If we created a class, such as Bottone it is correct to associate with this one or more events through a special class (derived from the class Event BottoneEvent We could make all the events that affect the functioning of our subject: the click the mouse over, or any other event! Well as defining our personal constant. The skeleton - generic - a custom event class is:
- 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 .*;
- / *
- ** @ Name: BottoneEvent
- ** @ Description: constructor
- * /
- type , bubbles , cancelable ) ; super (type, bubbles, cancelable);
- Value = v ; this. Value = v;
- )
- / *
- ** @ Override
- * /
- this . type , this . Value , this . bubbles , this . cancelable ) ; return new KnobEvent (this. type, this. Value, this. bubbles, this. cancelable);
- )
- )
- )
In the class Bottone the situation is even simpler! Take for example the portion of code that generates the event, within the class Bottone
- / *
- ** @ Name: myMethod ()
- * /
- mioMetodo ( ) : void { public function myMethod (): void (
- / / Something happens and we need to generate an event
- / / Also pass a value
- new BottoneEvent ( BottoneEvent . MIOEVENTO , 1234 ) ) ; dispatchEvent (new BottoneEvent (BottoneEvent. MIOEVENTO, 1234));
- )
Application side, of course, we will use our class Bottone like all the others:
- import buttons;
- import BottoneEvent;
- / /
- 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 (e. type + '/' + e. Value) / / 'bar / 1234'
- )
- )













There are no comments for this post
Leave a comment