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

Brian Hodge picture Brian Hodge · Mar 23, 2009 · Viewed 15.1k times · Source

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

Answer

Philippe picture Philippe · May 9, 2009

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
          }
     }
}