Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3 [Event(name="", type="")], what is the significance?

I develop with FlashDevelop3 R2 and the Flex 3.3 SDK and there are many occasions where I must use the embed metadata tag as such:

  [Embed(source="path/to/file")]
  private var Asset:Class;

I understand the above all well and good, and I am thankful it exists because I do not like to open the flash IDE too often.

When I am going through other authors classes lately, I have found an interesting metadata tag that I do not understand:

[Event(name="", type="")]

I have yet to see a situation where I require this, and furthermore I really just do not understand what it is for.

Thank in advance for your help.

Brian Hodge
blog.hodgedev.com hodgedev.com

like image 975
Brian Hodge Avatar asked Mar 23 '09 22:03

Brian Hodge


2 Answers

These [Event(name, type)] declarations describe which events a class instance is likely to dispatch.

They are actually useful for code completion - for instance when you type: mySprite.addEventListener(, your code editor (Flex Builder or FlashDevelop) will display a meaningful list of events that this object can dispatch.

So you can add these declarations in your code and benefit from a richer code completion.

Also note that this works with custom Event classes (see FlashDevelop's new Event class template).

package mycomp {
    import flash.events.Event;

    public class MyEvent extends Event {
         public const SOME_EVENT:String = "someEvent";
         // required Event type declarations
    }
}

package mycomp {
     [Event(name="someEvent", type="mycomp.MyEvent")]
     public class MyComp extends Sprite {
     }
}

package myproject {
     import mycomp.MyComp;

     public class MyProject {
          function MyProject() {
                var mc:MyComp = new MyComp();
                mc.addEventLister( //completes: SOME_EVENT + Sprite events
          }
     }
}
like image 131
Philippe Avatar answered Sep 17 '22 20:09

Philippe


We use it for binding custom events to our custom MXML components. This tag allows you to reference it from MXML. See documentation:

[Event(name="enableChanged", type="flash.events.Event")]

class ModalText extends TextArea { ... }

<MyComp:ModalText enableChanged="handleEnableChangeEvent(event);"/>

The compiler will complain, however, if you try to refer to an event on an mxml tag that was not declared with an event metatag.

like image 40
dirkgently Avatar answered Sep 21 '22 20:09

dirkgently