flash as3中怎样监测同时按下两个键?
2个回答
展开全部
在平常的思路中,就是当按↑的时候,让物体的y坐标减少,依次类推。但是这样只能让物体在上下左右四个固定的方向移动。所以要换个思路,改变策略。
var px_speed:Number = 0; //飞机的x轴速度
var py_speed:Number = 0; //飞机的y轴速度
stage.addEventListener(KeyboardEvent.KEY_DOWN,DownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP,UpHandler);
function DownHandler(e:KeyboardEvent){
switch(e.keyCode){
case Keyboard.LEFT:
px_speed = -10
break;
case Keyboard.RIGHT:
px_speed = 10;
break;
case Keyboard.UP:
py_speed = -10;
break;
case Keyboard.DOWN:
py_speed = 10;
break;
}
}
function UpHandler(e:KeyboardEvent){
px_speed = 0;
py_speed = 0;
}
//myPlane 就是飞机元件
myPlane.addEventListener(Event.ENTER_FRAME,movePlane);
function movePlane(e:Event){
myPlane.x += px_speed;
myPlane.y += py_speed;
//可以继续添加你想要的代码效果
}
var px_speed:Number = 0; //飞机的x轴速度
var py_speed:Number = 0; //飞机的y轴速度
stage.addEventListener(KeyboardEvent.KEY_DOWN,DownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP,UpHandler);
function DownHandler(e:KeyboardEvent){
switch(e.keyCode){
case Keyboard.LEFT:
px_speed = -10
break;
case Keyboard.RIGHT:
px_speed = 10;
break;
case Keyboard.UP:
py_speed = -10;
break;
case Keyboard.DOWN:
py_speed = 10;
break;
}
}
function UpHandler(e:KeyboardEvent){
px_speed = 0;
py_speed = 0;
}
//myPlane 就是飞机元件
myPlane.addEventListener(Event.ENTER_FRAME,movePlane);
function movePlane(e:Event){
myPlane.x += px_speed;
myPlane.y += py_speed;
//可以继续添加你想要的代码效果
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询