AS3在动画状态之间切换

时间:2015-04-21 20:35:30

标签: actionscript-3 animation

我在编写Flash中的动画状态变化时遇到了麻烦。我有一个小的大行星风格的游戏,3个水平行,角色可以跳跃。我在补间中设置了动画,但是我无法让角色从运行动画转换为跳回/跳转动画,然后在完成图层之间的转换时返回运行动画。

运行动画帧是1 - 60,跳回一层是61,向前跳一层是62.我还没有开始担心静止,所以它没有帧。

编辑:我删除了anmatePlayer函数并将动画控制代码移动到checkEveryFrame函数中。我只为动画制作了一个单独的功能,因为我不确定它应该去哪里。

package
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import fl.transitions.Tween;
    import fl.transitions.easing.*;
    import fl.transitions.TweenEvent;

    public class Main_Test_2 extends MovieClip
    {
        // variables
        private var cam:MovieClip = new MovieClip();
        private var player:Player = new Player();
        private var topPosition:uint = 250;
        private var centerPosition:uint = 430;
        private var bottomPosition:uint = 610;
        private var tweenSpeed:Number = 0.33;
        private var UI:UserInterface = new UserInterface();

        public function Main_Test_2():void
        {
            // initialize
            init();
        }
        public function init():void
        {   
            // initialize variables
            stage.addChild (cam);
            player.x = 200;
            player.y = bottomPosition;
            cam.addChild (UI);
            cam.addChild (player);

            // add event listeners
            stage.addEventListener(Event.ENTER_FRAME, checkEveryFrame);
            UI.topButton.addEventListener(MouseEvent.CLICK, topButtonClick);
            UI.centerButton.addEventListener(MouseEvent.CLICK, centerButtonClick);
            UI.bottomButton.addEventListener(MouseEvent.CLICK, bottomButtonClick);
        }

        public function checkEveryFrame(event:Event):void
        {
            cam.x -= player.x - player.x;
            cam.y -= player.y - player.y;
            player.vx = player.x - player.prevX;
            player.vy = player.y - player.prevY;
            player.prevX = player.x;
            player.prevY = player.y;

            /*if (player.y != topPosition || player.y != centerPosition || player.y != topPosition) 
            {
                player.gotoAndStop (61);
            }
            else if (player.y == topPosition || player.y == centerPosition || player.y == topPosition) 
            {
                player.gotoAndStop (1);
            }*/

            if (player.currentFrame == 60)
            {
                player.gotoAndStop (1);
            }
            else if (player.currentFrame < 60)
            {
                player.gotoAndStop (player.currentFrame + 1);
            }

            /*if (player.y < player.prevY) 
            {
                player.gotoAndStop (61);
            }
            else if (player.y > player.prevY)
            {
                player.gotoAndStop (62);
            }
            else if (player.vy == 0) 
            {
                player.gotoAndStop (1);
            }*/
        }

        public function topButtonClick (event:MouseEvent):void
        {
            trace ("Top Click");
            if (player.y >= bottomPosition)
            {
                var tween01:Tween = new Tween (player, "y", None.easeNone, bottomPosition, centerPosition, tweenSpeed, true);
            }
            else if (player.y == centerPosition)
            {
                var tween02:Tween = new Tween (player, "y", None.easeNone, centerPosition, topPosition, tweenSpeed, true);
            }
            else if (player.y < topPosition)
            {
                player.y = topPosition;
            }
        }
        public function centerButtonClick (event:MouseEvent):void
        {
            trace ("Center Click");
            if (player.y > centerPosition)
            {
                var tween01:Tween = new Tween (player, "y", None.easeNone, bottomPosition, centerPosition, tweenSpeed, true);
            }
            else if (player.y < centerPosition)
            {
                var tween03:Tween = new Tween (player, "y", None.easeNone, topPosition, centerPosition, tweenSpeed, true);
            }
        }
        public function bottomButtonClick (event:MouseEvent):void
        {
            trace ("Bottom Click");
            if (player.y <= topPosition)
            {
                var tween03:Tween = new Tween (player, "y", None.easeNone, topPosition, centerPosition, tweenSpeed, true);
            }
            else if (player.y == centerPosition)
            {
                var tween4:Tween = new Tween (player, "y", None.easeNone, centerPosition, bottomPosition, tweenSpeed, true);
            }
            else if (player.y > bottomPosition)
            {
                player.y = bottomPosition;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

查看实际调用animatePlayer的代码会很有帮助;无论如何,下面是一个解决方案。

不是依靠MovieClip制作动画,而是在现有的checkEveryFrame中处理它,从而为您提供更精确的控制,以便在您需要时准确地更改动画状态。我已经冒昧地将您的UI点击操作统一到一个更易于维护的功能中。此外,由于缺少您的键绑定功能,我为了演示而添加了playerState更改到用户界面。

尽我所知,这应该编译,但是如果没有剩下的对象,我就无法尝试。

package {
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import fl.transitions.Tween;
    import fl.transitions.easing.*;
    import fl.transitions.TweenEvent;

    public class Main_Test_2 extends MovieClip 
        // variables
        private var cam:MovieClip = new MovieClip();
        private var player:Player = new Player();
        private var tweenSpeed:Number = 0.33;
        private var UI:UserInterface = new UserInterface();

        public function Main_Test_2():void {
            // initialize
            init();
        }

        public function init():void {   
            // initialize variables
            stage.addChild(cam);
            player.x = 200;
            player.y = playerTracks[0];
            cam.addChild(UI);
            cam.addChild(player);

            // add event listeners
            stage.addEventListener(Event.ENTER_FRAME, checkEveryFrame);
            UI.topButton.addEventListener(MouseEvent.CLICK, movePlayer);
            UI.centerButton.addEventListener(MouseEvent.CLICK, movePlayer);
            UI.bottomButton.addEventListener(MouseEvent.CLICK, movePlayer);
        }

        public function checkEveryFrame(event:Event):void {
            cam.x -= player.x - player.x;
            cam.y -= player.y - player.y;
            player.vx = player.x - player.prevX;
            player.vy = player.y - player.prevY;
            player.prevX = player.x;
            player.prevY = player.y;

            // Animation control
            var a:Array = animations[playerState]; // current animation set
            if (a.length == 2) {
                var maxFrames:int = a[1] - a[0]; // Normalize the max value
                var currentFrame:int = player.currentFrame - a[0]; // Normalize the current value
                var nextFrame:int = normalizedCurrentFrame%maxFrames + a[0] // Modulus of max, and return to actual
                player.gotoAndStop(nextFrame)
            }
        }

        private var playerState:String = "run";
        private var animations:Object = {
            "run":[1,60],
            "jump":[61],
            "jumpForward":[62]
        }

        private var playerTracks:Array = [610, 430, 250];
        private var playerLocIndex:int = 0;

        public function movePlayer(e:MouseEvent):void {
            // Based on the button clicked, increase/decrease/set the index of the destination
            switch (e.currentTarget) {
                case UI.topButton:
                    playerState = "jump";
                    playerLocIndex++;
                    if (playerLocIndex > 2) { playerLocIndex = 2; } // Clamp upper
                    break;
                case UI.centerButton:
                    playerState = "run";
                    playerLocIndex = 1;
                    break;
                case UI.bottomButton:
                    playerState = "jumpForward";
                    playerLocIndex--;
                    if (playerLocIndex < 0) { playerLocIndex = 1; } // Clamp lower
                    break;
            }

            new Tween(player, "y", None.easeNone, player.y, playerTracks[playerLocIndex], tweenSpeed, true);
        }
    }
}