开发者

Call to possibly undefined method addEventListener

开发者 https://www.devze.com 2023-03-27 19:29 出处:网络
For some reason I can\'t add event listeners.. I\'ve searched the error but people say it\'s due to开发者_StackOverflow having the addEventListener outside of a function. However mine is inside the co

For some reason I can't add event listeners.. I've searched the error but people say it's due to开发者_StackOverflow having the addEventListener outside of a function. However mine is inside the constructor!

package {
import flash.events.*;

public class keyClass {
    var keyArray:Array = new Array  ;

    public function keyClass() {
        for (var i = 0; i < 999; i++) {
            keyArray[i] = false;
        }
        addEventListener(KeyboardEvent.KEY_DOWN,onKyDwn);
        addEventListener(KeyboardEvent.KEY_UP,onKyUp);
    }
    public function onKyDwn(e:KeyboardEvent) {
        keyArray[e.keyCode] = true;
    }
    public function onKyUp(e:KeyboardEvent) {
        keyArray[e.keyCode] = false;
    }
}

}


It is because addEventListener is a function of the class EventDispatcher, which you must extend to use it.

So, in your case, change your class definition to:

    public class keyClass extends EventDispatcher {

Also, don't bother looking into the IEventDispatcher interface. For reasons unknown it has been made unimplementable. The only way to get native event functionality is by extending EventDispatcher.

0

精彩评论

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

关注公众号