views:

2449

answers:

2

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

+3  A: 

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.

dirkgently
Interesting, I have been finding this in AS3 projects that include no MXML, do you think this is because this class may be imported into and used in a project that utilizes MXML?
Brian Hodge
Yes, custom components are typically coded in pure AS3 to avoid performance issues.
dirkgently
+2  A: 

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