实现的代码如下:
var distance:Number = 10;
this.createTextField("display_txt", 999, 0, 0, 100, 20);
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
if (Key.isDown(Key.LEFT)) {
car_mc._x = Math.max(car_mc._x - distance, 0);
display_txt.text = "Left";
} else if (Key.isDown(Key.RIGHT)) {
car_mc._x = Math.min(car_mc._x + distance, Stage.width - car_mc._width);
display_txt.text = "Right";
} else if (Key.isDown(Key.UP)) {
car_mc._y = Math.max(car_mc._y - distance, 0);
display_txt.text = "Up";
} else if (Key.isDown(Key.DOWN)) {
car_mc._y = Math.min(car_mc._y + distance, Stage.height - car_mc._height);
display_txt.text = "Down";
}
};
Key.addListener(keyListener);
注:该段代码最有趣的是边界检测部分的代码,可以算是边界检测时的一种新思路吧。