I encountered a problem, because I need to be extending MovieClip and EventDispatcher as well to catch events. But I have no idea how to do it.
Here is my class:
package
{
import flash.display.MovieClip;
import flash.events.*;
public class Character extends MovieClip //need to extend EventDispatcher as well
{
addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
var PressedKeys:Array = new Array();
public function update()
{
//.....
}
private function onKeyDown(event:KeyboardEvent):void
{
PressedKeys[event.keyCode] = true;
trace("Keycode: " + event.keyCode + " is: " + PressedKeys[event.keyCode]);
}
private function onKeyUp(event:KeyboardEvent):void
{
PressedKeys[event.keyCode] = false;
trace("Keycode: " + event.keyCode + " is: " + PressedKeys[event.keyCode]);
}
}
}
Adding your event listeners is not executed since you have not placed the code in a function.
Try adding your event listeners to your class constructor:
package
{
import flash.display.MovieClip;
import flash.events.*;
public class Character extends MovieClip //need to extend EventDispatcher as well
{
private var PressedKeys:Array = new Array();
public function Character()
{
addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
public function update()
{
//.....
}
private function onKeyDown(event:KeyboardEvent):void
{
PressedKeys[event.keyCode] = true;
trace("Keycode: " + event.keyCode + " is: " + PressedKeys[event.keyCode]);
}
private function onKeyUp(event:KeyboardEvent):void
{
PressedKeys[event.keyCode] = false;
trace("Keycode: " + event.keyCode + " is: " + PressedKeys[event.keyCode]);
}
}
}
ActionScript does not feature multiple inheritance - only single inheritance of base classes.
However, MovieClip
already extends EventDispatcher
.