2015-08-16 58 views
0

当两个键同时按下时,我的播放器停止移动。但动画仍在移动。例如,如果我同时上下左右按下或同时左右按下。as3 - 两个按键按下时如何停止动画?

On键按下事件侦听器:

if (event.keyCode == Keyboard.D) 
     { 
      isRight = true 
     } 
     if (event.keyCode == Keyboard.A) 
     { 
      isLeft = true 
     } 
     if (event.keyCode == Keyboard.W) 
     { 
      isUp = true 
     } 
     if (event.keyCode == Keyboard.S) 
     { 
      isDown = true 
     } 

在关键了事件侦听器:

if (event.keyCode == Keyboard.D) 
     { 
      isRight = false 
      gotoAndStop(1); 
     } 
     if (event.keyCode == Keyboard.A) 
     { 
      isLeft = false 
      gotoAndStop(1); 
     } 
     if (event.keyCode == Keyboard.W) 
     { 
      isUp = false 
      gotoAndStop(1); 
     } 
     if (event.keyCode == Keyboard.S) 
     { 
      isDown = false 
      gotoAndStop(1); 
     } 

在enterFrame事件:

if (isRight == true) 
     { 
      x += 5; 
      play(); 
     } 
     if (isLeft == true) 
     { 
      x -= 5; 
      play(); 
     } 
     if (isUp == true) 
     { 
      y -= 5; 
      play(); 
     } 
     if (isDown == true) 
     { 
      y += 5; 
      play(); 
     } 
+0

难道我们没有解决的东西昨天相似?在将Key down监听器设置为true之前,请将所有内容都设为false。看起来事情依然如此。 –

+0

如果我这样做,那种混乱了我的运动。我决定保持这样。 – Crook

+0

好的,在这种情况下,在你的钥匙键上放一些痕迹,看看所有的值都保持真实吗?如果你把所有东西都设为false,它是如何混淆你的代码的? –

回答

1

如果玩家进入X - = 1和x + = 1,它基本上使X + = 0的整体。我们可以很容易检查并在必要停止动画:

var iP:Point = new Point(x,y);//try to avoid creating new objects on frame interval 
if (isRight) x += 5; 
if (isLeft) x -= 5; 
if (isUp) y -= 5; 
if (isDown) y += 5; 
if(!Point.distance(iP,new Point(x,y)) goToAndStop(1); 
else play(); 
0

我没有看到任何检查,看是否多于一个键被按下?

想必你应该引入像一个keycount到enterFrame事件:

var count:uint = 0; 

if (isRight == true){ 
    count++ 
    x += 5; 
} 
if (isLeft == true){ 
    count++; 
    x -= 5; 
} 
if (isUp == true){ 
    count++; 
    y -= 5; 
} 
if (isDown == true){ 
    count++ 
    y += 5; 
} 

if (count > 1) { 
    isRight = isLeft = isUp = isDown = false; 
    gotoAndStop(1); 
} else { 
    play(); 
} 
+0

等一下,但我试过这种方法,然后当两个键被按下时,我的播放器开始移动。 – Crook

+0

我已经调整过,所以这个剧本是有条件的。 如果当两个键被按下,那么你绝对应该把跟踪语句到播放器中还是移动的if/else,看看有什么被解雇和时间。 另外,我会从上听者中删除'gotoAndStop(1)',并让输入框处理所有的时间线逻辑。 – Visualife

相关问题