smulholland2
smulholland2

Reputation: 1163

How do I addEventListener to custom actionscript class?

I have an actionscript class MyClass that extens NavigatorContent. I instantiate the class as a custom MXML NavigatorContnent component for an Accordion component. MyClass has a Button component that I have tried to attach an event listener to. I want the event to bubble so that I can have the handler in the Accordion component.

MyClass

package comp
{

  import flash.events.Event;
  import flash.events.MouseEvent;

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

  public class MyClass extends NavigatorContent
  {

    public function MyClass()
    {
      super();
      btnSelect.addEventListener(MouseEvent.CLICK, selectClickDispatcher);
    }


    public function selectClickDispatcher(event:MouseEvent):void
    {
      event.currentTarget.dispatchEvent(new Event("selectEvent",true));
    }
  }
}

From here I have the instantiated component nested in the Accordion. I am pretty sure the problem is in this class definition because when I set a breakpoint at the selectClickHandler, the code does not break. In case I am wrong I will post the rest of the components.

Custom component named MySubComp.mxml

<comp:MyClass
    ...I have a few more spark components here and nothing else...
/>

Accordion

<mx:Accordion>

    <fx:Script> //omitted CDATA tags to save space
        protected function selectEventHandler(event:Event):void
        {
            Alert.show("Value Selected");   
        }
    </fx:Script>

        //custom components are in the navs package
        <navs:MySubComp selectEvent = "selectEventHandler(event)"/>

</mx:Accordion>

Upvotes: 1

Views: 4217

Answers (3)

n4pgamer
n4pgamer

Reputation: 624

In case someone needs the real Actionscript-3 event dispatching to be used >>> this <<< is very helpful. I don't know if it is really slow but it meets the AS-3 standards.

Upvotes: 1

pho
pho

Reputation: 25489

You have added the metadata to the class definition

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

so all you need to do in mxml is

<comp:MyClass selectEvent="event_handler(event)"
..... />

In AS3, you add an event listener by

myClass.addEventListener("selectEvent", event_handler);

P.S. Your class will have to extend EventDispatcher

Upvotes: 2

divillysausages
divillysausages

Reputation: 8033

Your class either needs to extend a DisplayObject class, or directly inherit from EventDispatcher in order to be able to use events. Forget about implementing IEventDispatcher as there's a special piece of black code somewhere that means that EventDispatcher is the only class that can set the target property of the Event class (I've tried it before).

Consider using other alternatives. Events in Flash tend to be slow and create objects all the time. Callbacks are a good system if you need something simple.

public class A
{
    public var onSomething:Function = null;
    public function foo():void
    {
        if( this.onSomething != null )
            this.onSomething();
    }
}

public class B
{
    public function B()
    {
        var a:A = new A;
        a.onSomething = this._somethingCalled; // set the callback
        a.init();
    }

    private function _somethingCalled():void
    {
        trace( "hello there" );
    }
}

You can also take a look at the Signals project: https://github.com/robertpenner/as3-signals/wiki

Signals are vastly superior to normal Flash events, and there's no restriction on the type of object that can use them (i.e. non-DisplayObject objects can still add event listeners and dispatch events). It's also faster and has a smaller memory footprint.

Upvotes: 1

Related Questions