views:

108

answers:

2

I notice in actionscript 3 that when a handler gets called, its able to access the senders properties. If I dispatch an event from my custom class. can all the listeners inherit all my public properties and methods?

+1  A: 

If your handler method is defined in the same class as the dispatching call then you can access the properties, as they are in the scope. This is not generally the case - if you add an event handler to a button's "click" event for example - then you only have access to the properties of the button through a reference to that button (such as event.target). If you are more familiar with JS or AS2, then I understand why you are confused - in JS and AS2 the properties of an object is rarely in the scope of a method (you always need to use "this" to access instance properties for example).

class Foo extends EventDispatcher {
    public var myProperty: int;
    function thisMethodIsDispatchingEvents() {
        addEventListener("fooEvent", handleFooEvent);
        dispatchEvent(new Event("fooEvent"));
    }
    function handleFooEvent(event: Event) {
        // "myProperty" can be accessed because "handleFooEvent" 
        // is declared in Foo, not because handleFooEvent is an event handler
        trace(myProperty); // Works
    }
}

// in an unrelated class Bar:
class Bar {
    private var _foo: Foo;
    function bar() {
        _foo = new Foo();
        _foo.addEventListener("fooEvent", handleFooEvent);
    }
    function handleFooEvent(event: Event) {
        // "myProperty" can not be accessed directly, only though
        // a reference to a Foo object.
        trace(myProperty); // Fails
        trace(_foo.myProperty); // Works
        trace(event.target.myProperty); // Works
    }
}
Markus Johnsson
+1  A: 

Listener doesn't inherit the values, it can get a reference to the object that dispatched the event using the event.target property, or a reference to the object with which the event listener was registered using the event.currentTarget property. So basically, you can access the public properties of the object that dispatched event using the reference obtained from target/currentTarget property of the event.

function someFunction():void
{
  //abc is local variable and inaccessible outside someFunction
  var abc:ABC = new ABC();
  abc.prop = "someValue";
  abc.addEventListener(Event.TYPE, handleEvent);
}
function handleEvent(e:Event):void
{
  //get a reference to the object that was declared
  //in someFunction using the event.currentTarget.
  var abc:ABC = ABC(e.currentTarget);
  trace(abc.prop);//traces someValue
}

//some where in the ABC class:
this.dispatchEvent(new Event(Event.TYPE));

In case you are wondering, the event.target and event.currentTarget properties can be different. Add a click event listener to a sprite and click on a text field inside the sprite (which is a child of it) and check the values of target and currentTarget. Target will be the text field and the current target will be the sprite itself.

Amarghosh
I didnt use the proper word. I know that they dont inherit the properties and methods. What I meant was the dispatch sends a reference that can be used.
numerical25