Call to possibly undefined method addEventListener
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
.
精彩评论