1

我在游戏中完成跑步的方式是它检测到你点击了一个电影剪辑的跑步按钮,然后它设置了增加的步行速度。如果您抬起手指或将其从按钮上移开,它会将其恢复为默认步行速度。

因此,问题是运行按钮仅在定向 DPAD 之前按下时才起作用。

我该如何解决?

我的运动课

package 
{
    import flash.display.Stage;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.TouchEvent;
    import flash.net.dns.AAAARecord;
    import flash.ui.Multitouch;
    import flash.ui.MultitouchInputMode;


    public class Movement extends MovieClip
    {
        public function Movement(main:Game)
        {
            trace("SUCCESS | Constructed Movement Class");

            addChild(Game.playerPosKeeper_mc);
            Game.playerPosKeeper_mc.x = 384;
            Game.playerPosKeeper_mc.y = 46;

            addChild(main.up_dpad);
            main.up_dpad.x = 55;
            main.up_dpad.y = 336;

            addChild(main.down_dpad);
            main.down_dpad.x = 57;
            main.down_dpad.y = 432;

            addChild(main.left_dpad);
            main.left_dpad.x = 19;
            main.left_dpad.y = 372;

            addChild(main.right_dpad);
            main.right_dpad.x = 118;
            main.right_dpad.y = 372;

            addChild(main.menu_dpad);
            main.menu_dpad.x = 61;
            main.menu_dpad.y = 377;

            addChild(main.run_dpad);
            main.run_dpad.x = 684;
            main.run_dpad.y = 369;

            addChild(main.barrierRoof1_game);
            main.barrierRoof1_game.x = 0;
            main.barrierRoof1_game.y = 0;

            addChild(main.barrierRoof2_game);
            main.barrierRoof2_game.x = 0;
            main.barrierRoof2_game.y = 470;

            addChild(main.barrierRoof3_game);
            main.barrierRoof3_game.x = 0;
            main.barrierRoof3_game.y = 320;

            addChild(main.barrierSide1_game);
            main.barrierSide1_game.x = 0;
            main.barrierSide1_game.y = 0;

            addChild(main.barrierSide2_game);
            main.barrierSide2_game.x = 790;
            main.barrierSide2_game.y = 0;

            Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;

            main.run_dpad.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBeginRUN);
            main.run_dpad.addEventListener(TouchEvent.TOUCH_OUT, onTouchEndRUN);
            main.run_dpad.addEventListener(TouchEvent.TOUCH_END, onTouchEndRUN);

            function onTouchBeginRUN(e:TouchEvent):void
            {
                Game.upWalkspeed = -5;
                Game.downWalkspeed = 5;
                Game.leftWalkspeed = -5;
                Game.rightWalkspeed = 5;
            }
            function onTouchEndRUN(e:TouchEvent):void
            {
                Game.upWalkspeed = -3;
                Game.downWalkspeed = 3;
                Game.leftWalkspeed = -3;
                Game.rightWalkspeed = 3;
            }

            for each (var aButton:MovieClip in main.Buttons)
            {
                aButton.addEventListener(TouchEvent.TOUCH_BEGIN, onDown);
                aButton.addEventListener(TouchEvent.TOUCH_OUT, onUp);
                aButton.addEventListener(TouchEvent.TOUCH_END, onUp);
            }

            function onDown(e:TouchEvent):void
            {
                switch (e.currentTarget)
                {
                    case main.up_dpad :
                        Game.goingUp = true;
                        Game.goingDown = false;
                        Game.goingLeft = false;
                        Game.goingRight = false;
                        main._Direction.x = 0;
                        main._Direction.y = Game.upWalkspeed;


                        if (Game.player1)
                        {
                            if (P1UAnim_mc != null)
                            {
                            }
                            else
                            {
                                var P1UAnim_mc:MovieClip = new mc_P1UAnim();
                                addChild(P1UAnim_mc);
                            }
                        }
                        else if (Game.player2)
                        {
                            if (P2UAnim_mc != null)
                            {
                            }
                            else
                            {
                                var P2UAnim_mc:MovieClip = new mc_P2UAnim();
                                addChild(P2UAnim_mc);
                            }
                        }
                        break;

                    case main.down_dpad :
                        Game.goingUp = false;
                        Game.goingDown = true;
                        Game.goingLeft = false;
                        Game.goingRight = false;
                        main._Direction.x = 0;
                        main._Direction.y = Game.downWalkspeed;


                        if (Game.player1)
                        {
                            if (P1DAnim_mc != null)
                            {
                            }
                            else
                            {
                                var P1DAnim_mc:MovieClip = new mc_P1DAnim();
                                addChild(P1DAnim_mc);
                            }
                        }
                        else if (Game.player2)
                        {
                            if (P2DAnim_mc != null)
                            {
                            }
                            else
                            {
                                var P2DAnim_mc:MovieClip = new mc_P2DAnim();
                                addChild(P2DAnim_mc);
                            }
                        }
                        break;

                    case main.left_dpad :
                        Game.goingUp = false;
                        Game.goingDown = false;
                        Game.goingLeft = true;
                        Game.goingRight = false;
                        main._Direction.x = Game.leftWalkspeed;
                        main._Direction.y = 0;


                        if (Game.player1)
                        {
                            if (P1LAnim_mc != null)
                            {
                            }
                            else
                            {
                                var P1LAnim_mc:MovieClip = new mc_P1LAnim();
                                addChild(P1LAnim_mc);
                            }
                        }
                        else if (Game.player2)
                        {
                            if (P2LAnim_mc != null)
                            {
                            }
                            else
                            {
                                var P2LAnim_mc:MovieClip = new mc_P2LAnim();
                                addChild(P2LAnim_mc);
                            }
                        }
                        break;

                    case main.right_dpad :
                        Game.goingUp = false;
                        Game.goingDown = false;
                        Game.goingLeft = false;
                        Game.goingRight = true;
                        main._Direction.x = Game.rightWalkspeed;
                        main._Direction.y = 0;


                        if (Game.player1)
                        {
                            if (P1RAnim_mc != null)
                            {
                            }
                            else
                            {
                                var P1RAnim_mc:MovieClip = new mc_P1RAnim();
                                addChild(P1RAnim_mc);
                            }
                        }
                        else if (Game.player2)
                        {
                            if (P2RAnim_mc != null)
                            {
                            }
                            else
                            {
                                var P2RAnim_mc:MovieClip = new mc_P2RAnim();
                                addChild(P2RAnim_mc);
                            }
                        }
                        break;
                }
                if (! Game.inMotion)
                {
                    Game.inMotion = true;
                    addEventListener(Event.ENTER_FRAME, onFrame);
                }
            }

            function onFrame(e:Event)
            {
                movePlayer(main._Direction.x, main._Direction.y);
            }

            function onUp(e:TouchEvent):void
            {
                removeEventListener(Event.ENTER_FRAME, onFrame);

                Game.goingUp = false;
                Game.goingDown = false;
                Game.goingLeft = false;
                Game.goingRight = false;

                Game.inMotion = false;
                main._Direction.x = 0;
                main._Direction.y = 0;
            }

            function movePlayer(movementX:Number, movementY:Number):void
            {
                var originalX:Number = Game.playerPosKeeper_mc.x;
                var originalY:Number = Game.playerPosKeeper_mc.y;
                Game.playerPosKeeper_mc.x +=  movementX;
                if (checkCollision())
                {
                    Game.playerPosKeeper_mc.x = originalX;
                }
                Game.playerPosKeeper_mc.y +=  movementY;
                if (checkCollision())
                {
                    Game.playerPosKeeper_mc.y = originalY;
                }
            }

            function checkCollision():Boolean
            {
                for each (var StageCollisions:MovieClip in main.StageCollisions)
                {
                    if (Game.playerPosKeeper_mc.hitTestObject(StageCollisions))
                    {
                        return true;
                        Game.inMotion = false;
                    }
                }
                return false;
            }
        }
    }
}

编辑:

这是我做运动的方式:

有一个绑定到播放器坐标的影片剪辑。这就是动画设置它们的 x 和 y 坐标的原因。如果玩家开始移动,则 inMotion 变量变为真,这意味着玩家正在移动。玩家前进方向的变量也会改变(如果他向左移动 goLeft = true)

如果玩家在 DPAD 上击中某物或放开一个方向,则 inMotion 为假。

这样做是为了在适当的时间将动画添加到舞台,并在适当的时间进行动画处理。例如:

我按左 DPAD

inMotion = true,goingLeft = true

如果左侧动画不在舞台上,则将其添加到舞台上。

left 动画检测变量并相应地响应它们: inMotion && goingLeft 向左移动 !inMotion && !goingLeft 那时是空闲的,不要动画 inMotion && !goingLeft 正在向另一个方向移动,删除动画

我按右 DPAD 遵循上述相同的循环

这可以确保在正确的时间播放正确的动画,并且这段代码可能比它需要的更长,但这确实显示了我在代码中所知道的限制。

4

1 回答 1

0

只要您看到 2 块看起来相似的代码,就知道该代码很糟糕:https ://en.wikipedia.org/wiki/Duplicate_code 。更少的代码=更好的公式总是正确的。

空代码块会降低可读性:

// Bad.
if (condition)
{
}
else
{
    // some code
}

// Good.
if (!condition)
{
    // some code
}

如果case 使用逻辑&& 和逻辑|| ,您还可以将多个条件堆叠为一个。除非它变得不可读:

// Bad.
if (conditiona)
{
    if (conditionb)
    {
        if (conditionc)
        {
        }
        else
        {
            // some code
        }
    }
}

// Better.
if (conditiona && conditionb && !conditionc)
{
    // some code
}

如果您的目标是分配单个变量,而不是if的弹幕,您可以使用 ternar 运算符。同样,除非可读性下降:

var foo:*;

// Block of ifs.
if (conditiona)
{
    foo = 1;
}
else if (conditionb)
{
    foo = 2;
}

// Ternar assignment.
var foo:* = conditiona? 1: conditionb? 2: foo;

// Ternar assignment in a more readable form.
var foo:* = conditiona? 1: (conditionb? 2: foo);

因此,您的代码应用了以上所有内容:

        function setDirection(tox:Number, toy:Number):void
        {
            Game.goingUp    = (toy < 0);
            Game.goingDown  = (toy > 0);
            Game.goingLeft  = (tox < 0);
            Game.goingRight = (tox > 0);

            main._Direction.y = Game.goingUp  ? Game.upWalkspeed  : Game.goingDown ? Game.downWalkspeed : 0;
            main._Direction.x = Game.goingLeft? Game.leftWalkspeed: Game.goingRight? Game.rightWalkspeed: 0;
        }

        function onDown(e:TouchEvent):void
        {
            if (Game.player1 && !P1UAnim_mc)
            {
                var P1UAnim_mc:MovieClip = new mc_P1UAnim();
                addChild(P1UAnim_mc);
            }

            if (Game.player2 && !P2UAnim_mc)
            {
                var P2UAnim_mc:MovieClip = new mc_P2UAnim();
                addChild(P2UAnim_mc);
            }

            switch (e.currentTarget)
            {
                case main.up_dpad :
                    setDirection(0,-1);
                    break;

                case main.down_dpad :
                    setDirection(0,1);
                    break;

                case main.left_dpad :
                    setDirection(-1,0);
                    break;

                case main.right_dpad :
                    setDirection(1,0);
                    break;
            }

            if (!Game.inMotion)
            {
                Game.inMotion = true;
                addEventListener(Event.ENTER_FRAME, onFrame);
            }
        }

您还可以通过在 Flash IDE(Animate 或 CS6 或您拥有的任何一个)中设计 UI 来节省自己的 UI 布局编程。您在 Library 中设计 MovieClip,以便它在正确的位置拥有您需要的所有界面。您需要评估的所有 UI 元素都必须指定实例名称。然后为这个 MovieClip 创建一个类:

package
{
    public class Layout extends MovieClip
    {
        // Left, Right, Up and Down are instance names of the objects
        // in your designed MovieClip. Their accessors must be public.
        // Also you should understand what classes they should be:
        // SimpleButton for button object, TextField or TLFTextField
        // for texts, MovieClip or Sprite for containers.
        public var Left :SimpleButton;
        public var Right:SimpleButton;
        public var Up   :SimpleButton;
        public var Down :SimpleButton;

        // Then if you set everything right you can access then
        // in the class constructor with no need to create them
        // or set them up as they are already in their places by design.
        function Layout()
        {
            super();

            Left.addEventListener(...);
        }
    }
}
于 2017-03-13T10:02:50.447 回答