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:

ActionScript
  1. package (
  2. / *
  3. ** @ Name: BottoneEvent.as
  4. ** @ Description: Class derived from event to manage their own custom event
  5. ** @ Author: = undo =
  6. ** @ Web: http://www.undolog.com
  7. ** @ Email: @ g.fazioli undolog.com
  8. * /
  9. .*; import flash.events .*;
  10. BottoneEvent extends Event { public class BottoneEvent extends Event (
  11. String = 'pluto' ; // codice qualsiasi, anche 'mioevento' in minuscolo public static const MIOEVENTO: String = 'bar', / / any code, even 'mioevento' in lowercase
  12. Valore : Number = NaN ; public var value: Number = NaN;
  13. / *
  14. ** @ Name: BottoneEvent
  15. ** @ Description: constructor
  16. * /
  17. BottoneEvent ( type : String , v : Number , bubbles : Boolean = false , cancelable : Boolean = false ) : void { Public function BottoneEvent (type: String, v: Number, bubbles: Boolean = false, cancelable: Boolean = false): void (
  18. type , bubbles , cancelable ) ; super (type, bubbles, cancelable);
  19. Value = v ; this. Value = v;
  20. )
  21. / *
  22. ** @ Override
  23. * /
  24. clone ( ) : Event { override public function clone (): Event (
  25. this . type , this . Value , this . bubbles , this . cancelable ) ; return new KnobEvent (this. type, this. Value, this. bubbles, this. cancelable);
  26. )
  27. )
  28. )

In the class Bottone the situation is even simpler! Take for example the portion of code that generates the event, within the class Bottone

ActionScript
  1. / *
  2. ** @ Name: myMethod ()
  3. * /
  4. mioMetodo ( ) : void { public function myMethod (): void (
  5. / / Something happens and we need to generate an event
  6. / / Also pass a value
  7. new BottoneEvent ( BottoneEvent . MIOEVENTO , 1234 ) ) ; dispatchEvent (new BottoneEvent (BottoneEvent. MIOEVENTO, 1234));
  8. )

Application side, of course, we will use our class Bottone like all the others:

ActionScript
  1. import buttons;
  2. import BottoneEvent;
  3. / /
  4. Bottone = new Bottone ( ) ; var foo: Button = new Button ();
  5. ( BottoneEvent . MIOEVENTO , foo. addEventListener (BottoneEvent. MIOEVENTO,
  6. e : BottoneEvent ) : void { function (e: BottoneEvent): void (
  7. / / Do!
  8. e . type + ' / ' + e . Value ) ; // 'pluto / 1234' trace (e. type + '/' + e. Value) / / 'bar / 1234'
  9. )
  10. )

Related Post

Was this article helpful?: Per nientePocoAbbastanzaMoltoMoltissimo
Loading ... Loading ...

There are no comments for this post

Leave a comment

TAG XHTML PERMISSIONS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre>         // blocco generico [code][/code]       // blocco generico [as][/as]           // Actionscript [css][/css]         // CSS Style Sheet [html][/html]       // HTML [js][/js]           // Javascript [objc][/objc]       // Objective-C [php][/php]         // PHP [sql][/sql]         // SQL