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

2

14

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

Osrock answered 23/3, 2009 at 22:0 Comment(0)
E
12

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.

Erewhile answered 23/3, 2009 at 22:18 Comment(2)
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?Osrock
Yes, custom components are typically coded in pure AS3 to avoid performance issues.Erewhile
A
20

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
          }
     }
}
Anticholinergic answered 9/5, 2009 at 13:20 Comment(1)
Exactly the answer I was looking for!Allometry
E
12

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.

Erewhile answered 23/3, 2009 at 22:18 Comment(2)
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?Osrock
Yes, custom components are typically coded in pure AS3 to avoid performance issues.Erewhile

© 2022 - 2024 — McMap. All rights reserved.