views:

1058

answers:

2

I've done a lot of reading through forum posts and tutorials, but I still can't wrap my brain round 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 a repeater, when one of those objects is clicked, I want a different componenet to display data associated with that object.

Here's what I have, but the listener never responds (the dispatcher seems to be working though, because the new event is created and I can see the trace with the proper output.) I suspect it is because when I call addEvent Listener, I am doing so on the wrong object. My understanding is that the object that will display the object data is the object that should have the event listener, and listen for all events of this nature, 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 which is called on click:

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

And the component I want to display 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); 
     }
+1  A: 

It's not quite clear where your last two code chunks are, but it looks like you need to be calling addEventListener on the object that extends EventDispatcher.

That is, if your second chunk belongs to a custom object called Clickable, which extends EventDispatcher and calls dispatchEvent() when clicked, then your component should be calling myClickable.addEventListener(...) where myClickable is an instance of Clickable. Does that make sense?

But assuming your 3rd code chunk is not in the same class as the second, it doesn't look like you're doing that. You're adding a listener to the class that owns the third chunk of code, which I gather is not the one that extends EventDispatcher.

fenomas
Well... yes, the second chunk belongs to an object that dispatches the event, but it is not clickable, and it doesn't extend EventDispatcher as far as I know (it extends UIComponent.) It's a game object that sits in a List component. When that object in the list is selected, it dispatches its event. Does every object that dispatches an event have to extend EventDispatcher?
Martholomew
Maybe then my issue is that the List should dispatch the event, with the selected object in the argument?
Martholomew
Well, DisplayObject extends EventDispatcher, so anything you have on the stage will as well. I don't know who should dispatch the event - whoever knows when to do so, I suppose, but that's down to your code. What's definite is that if A dispatches the event, and B receives it, then your call needs to be someA.addEventListener( eventType, someB.method ). Right now you seem to have B adding an event listener to itself.
fenomas
Thanks. that was the problem. I was calling addEventListener on the wrong object.
Martholomew
A: 

Just a quick glance at your code and notice that your dispatchEvent second parameter is the class not the instance of the object. Shouldn't this be myObject?

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