开发者

Flash: Am i totally misunderstanding Event Listening?

开发者 https://www.devze.com 2022-12-22 19:40 出处:网络
I don\'t know why but i am having trouble getting my head round event dispatching. Take this for example.

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 thi开发者_JAVA技巧s 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?


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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消