views:

42

answers:

3

I don't know why but i am having trouble getting my head round event dispatching.

Take this for example.

someClass():Void{

    this.addEventListener("onChange",someObj);

}

Am i right in assuming this means that someClass is listening for an onChange event and when it gets it, it is then going to fire the onChange method on someObj?

Thanks, Kohan.

Addendum:

lo = new Object();
lo.click = function(evt){
    trace(evt.target.label + " clicked");
}
button1.addEventListener("click", lo);

Hopefully from here which i have found on this site: http://www.webwasp.co.uk/tutorials/keywords/addEventListener.php

You can see how i came to this assumption. lo is an Object, not a method, am i correct?

A: 

The second parameter for addEventListener is the function name of the function that will get called when that event is caught.

Jason W
I thought it's meant to be an object?
Kohan
Like here: http://www.webwasp.co.uk/tutorials/keywords/addEventListener.php
Kohan
You can set that up as a object with a defined onChange method. But that seems to be the older practice/convention.
Jason W
+2  A: 

You got it wrong:

someClass():Void {
    this.addEventListener("onChange",someObj);
}

Will add an event listener to this's onChange listeners list, which - when the event is fired - will call the someObj method!

You need to pass the method itself as the parameter. So, use:

someClass():Void {
    this.addEventListener("onChange",someObj.someMethod);
}

*BTW, it is better not to use the "onChange" string itself, but rather use constants (such as Event.ENTER_FRAME) which hold those strings.

M.A. Hanin
A: 

I think you are misunderstanding what an object is, the nameing reflects what the syntax means:

someMethod():Void {
     this.addEventListener("onChange",someOtherMethod);
}

someOtherMethod():Void {
     // something happens here when the "onChange" event is triggered
}

the syntax is this.addEventListener(name of event, the function that gets called);

obj = new Object();

obj.method = function(evt) {
     // something happened
}

otherObject.addEventListener("Event Name", this.obj);

otherObject.dispatchEvent("Event Name")

That is how the syntax works. But making your own events is a little more complex

Ross
I have added an example. Is that not an Object?
Kohan
second example yes. First no, because you have `():Void`
Ross
So would i have been correct in my assumption if it had no ():Void ?
Kohan