切换到第二个场景后,键盘事件在我左键单击屏幕之前不起作用 如何避免左键单击以便我可以在移动到第二个场景后直接触发我的键盘事件?
您可能需要的更多详细信息:
第一个场景
btnStart.addEventListener(MouseEvent.CLICK, initGame);
function initGame(e:MouseEvent) {
gotoAndPlay(1, "Gameplay");
}
第二场景
我只使用1帧。我有一个对象。我设置了键盘事件功能,将对象向右或向左移动。我stop()
框架。
在我给屏幕点击鼠标之前,我的键盘功能不起作用。
var moveUp:Boolean = false;
var moveDown:Boolean = false;
var moveLeft:Boolean = false;
var moveRight:Boolean = false;
var isMoving:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveCowboy);
function moveCowboy(e:KeyboardEvent) {
if (e.keyCode == Keyboard.W) {
moveUp = true;
if (isMoving == false)
mycowboy.gotoAndPlay(15);
isMoving = true;
} else if (e.keyCode == Keyboard.S) {
moveDown = true;
if (isMoving == false)
mycowboy.gotoAndPlay(15);
isMoving = true;
} else if (e.keyCode == Keyboard.A) {
moveLeft = true;
if (isMoving == false)
mycowboy.gotoAndPlay(15);
isMoving = true;
} else if (e.keyCode == Keyboard.D) {
moveRight = true;
if (isMoving == false)
mycowboy.gotoAndPlay(15);
isMoving = true;
}
}
stage.addEventListener(KeyboardEvent.KEY_UP, stopCowboy);
function stopCowboy(e:KeyboardEvent) {
if (e.keyCode == Keyboard.W) {
moveUp = false;
} else if (e.keyCode == Keyboard.S) {
moveDown = false;
} else if (e.keyCode == Keyboard.A) {
moveLeft = false;
} else if (e.keyCode == Keyboard.D) {
moveRight = false;
}
if (moveUp == false && moveDown == false && moveLeft == false && moveRight == false) {
if (isMoving == true)
mycowboy.gotoAndPlay(1);
isMoving = false;
}
}
var myTimer:Timer = new Timer(20);
myTimer.addEventListener(TimerEvent.TIMER, myTimerFunc);
function myTimerFunc(e:Event):void {
//cowboy movement
if (moveUp) {
mycowboy.y -= 3;
mycowboy.rotation = 0;
}
else if (moveDown) {
mycowboy.y += 3;
mycowboy.rotation = 180;
}
else if (moveLeft) {
mycowboy.x -= 3;
mycowboy.rotation = -90;
}
else if (moveRight) {
mycowboy.x += 3;
mycowboy.rotation = 90;
}
}
myTimer.start();
stop();
答案 0 :(得分:0)
在代码末尾添加此内容
stage.focus = stage;