Adding a custom event listener in as3

I read a lot in the posts and tutorials on the forum, but I still can't wrap my brain around events and event listeners. I have a pretty simple example, but I can't get it to work.

I have an arrayCollection of custom objects in the repeater, when one of these objects is clicked, I want the other component to display the data associated with this object.

I have it here, but the listener never answers (the dispatcher seems to work because a new event has been created, and I see the trace with the correct exit.) I suspect this is happening, because when I call addEvent Listener, I do it on the wrong object. I understand that the object that will display the data of the object is the object that the event listener should have and listen to all events of this kind, but maybe I misunderstood.

My custom event:

public class SelectObjectEvent extends Event
    {
        public function SelectObjectEvent(customEventString:String, myObject:customObject)
        {
              super(customEventString, true, false);
              trace(customEventString+" "+myObject);
        }
    }
}

My custom object has the following function that is called when clicked:

public function selectObject(myObject:customObject):void
        {
            dispatchEvent(new SelectObjectEvent("OBJECT_SELECTED", customObject));              
        }

And the component that I want to display for the selected object has the following constructor:

public function SelectedObjectDisplayClass()
        {
            addEventListener("OBJECT_SELECTED", this.showObject)
        }

        public function showObject(event:Event):void
            {  
            trace("Show object: "+event); 
        } 
+5
source share
2 answers

, , , addEventListener , EventDispatcher.

, Clickable, EventDispatcher dispatchEvent() , myClickable.addEventListener(...), myClickable Clickable. ?

, , , . , , , , EventDispatcher.

+3

, dispatchEvent - , . myObject?

public function selectObject(myObject:customObject):void
        {
                dispatchEvent(new SelectObjectEvent("OBJECT_SELECTED", **customObject**));                          
        }
0

All Articles