【问题标题】:AS3 Animation stop at frame 1AS3 动画在第 1 帧处停止
【发布时间】:2013-02-02 09:48:23
【问题描述】:

我真的是 AS3 的新手,我曾经在 AS2 中编码,但一年多以来我没有使用 Flash 或 ActionScript。 我的问题是当我按下左箭头或右箭头时,该箭头被拒绝将角色向右移动,而动画仅在第一帧处停止。空闲动画效果很好,但每次按下按钮时,步行动画都会在第 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”是我的行走动画。

【问题讨论】:

    标签: actionscript-3 flash animation actionscript


    【解决方案1】:

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

    • rightKeyDown 以 false 开头。该分支中没有代码被执行。

    • 用户按住右箭头,rightKeyDown变为真

    • moverChar 检查rightKeyDown,发现它是真的并将字符发送给“andando”。

    • moveChar 再次运行,看到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");
    }
    

    【讨论】:

    • 谢谢,伙计。现在工作正常,我真的被这个问题困住了。但是你救了我,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2022-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多