keyboard的event只能绑定到stage上 建议不用focus地方法把事件绑定到mc上
下边是一个文档类的内容
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
public class DrawingDemo extends Sprite {
public var actor:Actor = new Actor();
// Constants:
// Public Properties:
// Private Properties:
// Initialization:
public function DrawingDemo() {
init();
}
private function init():void {
graphics.lineStyle(10);
stage.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownHandler);
stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUpHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
actor.x=100;
actor.y=200;
actor.scaleX=2.5;
addChild(actor);
}
function keyDownHandler(event:KeyboardEvent):void {
trace("11223123123");
if (event.keyCode==37) {
actor.Left();
} else if (event.keyCode==39) {
actor.Right();
} else if (event.keyCode==38) {
actor.Up();
} else if (event.keyCode==40) {
actor.Down();
}
}
private function onMouseDownHandler(event:MouseEvent):void {
graphics.moveTo(mouseX,mouseY);
stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMoveHandler);
}
private function onMouseUpHandler(event:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMoveHandler);
}
private function onMouseMoveHandler(event:MouseEvent):void {
graphics.lineTo(mouseX,mouseY);
}
}
}