2013-02-18 58 views
0

我真的是AS3的新手,我曾经在AS2编码,但是一年多的时间我不使用Flash或ActionScript。 我的问题是,当我按左或右箭头,将左右角色左右移动时,动画就停在第一帧。空闲的动画可以正常工作,但每次按下按钮时,步行动画就会在第1帧中开始和停止。AS3动画在第1帧停止

vector.gotoAndPlay("parado"); 

var leftKeyDown:Boolean = false; 
var rightKeyDown:Boolean = false; 
var mainSpeed:Number = 7; 

vector.addEventListener(Event.ENTER_FRAME, moveChar); 
function moveChar(event:Event):void{ 

    if(leftKeyDown){ 
     if(vector.currentLabel!="andando"){ 
      vector.x -= mainSpeed; 
      vector.scaleX=-1; 
      vector.gotoAndPlay("andando"); 
     } 
    } else { 
     if(rightKeyDown){ 
      if(vector.currentLabel!="andando") { 
       vector.x += mainSpeed; 
       vector.scaleX=1; 
       vector.gotoAndPlay("andando"); 
      } 
     } 
    } 
} 

stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown); 
function checkKeysDown(event:KeyboardEvent):void{ 

    if(event.keyCode == 37){ 
     leftKeyDown = true; 
    } 

    if(event.keyCode == 39){ 
     rightKeyDown = true; 
    } 
    } 
    stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp); 
    function checkKeysUp(event:KeyboardEvent):void{ 

    if(event.keyCode == 37){ 
     leftKeyDown = false; 
    } 
    if(event.keyCode == 39){ 
     rightKeyDown = false; 
    } 
} 

仅供参考:“parado”是我的空闲动画,“andando”是我的步行动画。

回答

3

它不停在第1帧,它只是一直发送回第1帧。考虑按住按钮几秒后会发生什么情况:

  • rightKeyDown开始为假。该分支中没有代码被执行。

  • 用户持有的右箭头,rightKeyDown成为真正

  • moverChar检查rightKeyDown,认为它是真实的,并发送字符“andando”。

  • moveCharmoveChar再次运行,看到rightKeyDown是真的,但角色仍然在“andando”框架,所以它什么也没有。

  • 字符在“andando”之后进入帧。

  • moverChar运行,rightKeyDown仍然如此,但框架不再处于“andando”状态,所以它重新回到它。

,并在用户按住该键的所有时间重复,所以它似乎被卡住框1和2个

几个备选方案来解决这个问题:


仅当用户按下或释放按钮时才更改关键帧,而不是每个帧之间。

function moveChar(event:Event):void{ 

    if(leftKeyDown){ 
     vector.x -= mainSpeed; 
     // No frame checks or frame changes here. 
    } 
    [...] 

function checkKeysDown(event:KeyboardEvent):void{ 
    if(event.keyCode == 37){ 
     leftKeyDown = true; 
     vector.scaleX=-1; 
     vector.gotoAndPlay("andando"); 
     // Send the character to the correct frame when the user presses the key. 
    } 
    [...] 

function checkKeysUp(event:KeyboardEvent):void{ 
    if(event.keyCode == 37){ 
     leftKeyDown = false; 
     vector.gotoAndPlay("parado"); 
     // Send it back to idle when the user releases the key. 
    } 
    [...] 

另一种选择是每个动画存储本身就是一个影片剪辑,并把它们放在一个容器影片剪辑。所以角色符号中只有两个框架,一个用于空闲动画,另一个用于步行动画。在你的代码中,你使用gotoAndStop而不是gotoAndPlay,所以每帧调用它并不重要。


编辑:也尝试分组您的条件。

} else { 
    if(rightKeyDown){ 
     if(vector.currentLabel!="andando") { 
      vector.x += mainSpeed; 
      vector.scaleX=1; 
      vector.gotoAndPlay("andando"); 
     } 
    } 
} 

可以改写为

} else if (rightKeyDown && vector.currentLabel != "andando"){ 
    vector.x += mainSpeed; 
    vector.scaleX=1; 
    vector.gotoAndPlay("andando"); 
} 
+0

谢谢,伙计。现在工作正常,我真的被困在这个问题上。但是你救了我,谢谢你! – CptAwesome 2013-02-18 18:04:04