【问题标题】:Pressing run button does not increase speed unless pressed before除非之前按下,否则按下运行按钮不会提高速度
【发布时间】:2017-07-25 16:16:08
【问题描述】:

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

因此,问题在于运行按钮仅在定向 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

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

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

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

这样可以确保在正确的时间播放正确的动画,并且这段代码可能比 它必须是,但这诚实地表明了我在代码中所知道的限制。

【问题讨论】:

  • 上次我建议您开始以算法方式处理您的代码:stackoverflow.com/a/42421460/4687633。请考虑这样做,您的代码充满了不必要的重复块,并且在其中找到某些内容本身就是一个问题,更不用说排除问题了。
  • 您好,我不知道您以算法方式编写代码是什么意思。
  • 谷歌 > 算法思维。多读书,多学。为什么?因为如果你不这样做,你就会淹没在自己代码的海洋中。我已经在另一个线程中解释了你的代码有什么问题,继续推动你的方式会让你一事无成。如您所见,没有人敢在这里分析您的问题,原因(可能是唯一原因)是您的代码是实际需要的 3-4 倍,因此很难了解。
  • 我用一些可能有用的信息更新了帖子,因为我不想在我的声明中乱扔垃圾。
  • 1.您仍然有 4 段完全相同的代码,每个 switch 块案例一个。 2.用脚本而不是设计来创建UI布局有什么意义吗?

标签: actionscript-3 flash actionscript air cs3


【解决方案1】:

只要您看到 2 块看起来相似的代码,就知道该代码是错误的:https://en.wikipedia.org/wiki/Duplicate_code更少的代码 = 更好这个公式永远是对的。

空代码块降低可读性:

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

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

您还可以使用逻辑 and && 和逻辑 or || 将多个条件叠加到一个 if 案例中除非它变得不可读:

// 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(...);
        }
    }
}

【讨论】:

  • Game.goingUp = (toy
  • 更具体地说是关于逻辑的东西,如果它更大,它本质上是否忽略了玩具的价值?我假设如果 Game.goingUp 小于 toy,Game.goingUp 将等于 toy。
  • 另外,我注意到您将 if 语句放在 switch 之外。这实质上会破坏动画,因为 P1UAnim_mc 将始终是正在使用的动画,这就是为什么我将这些语句放在 switch up/down/left/right 中的原因,因为这些是检查动画是否等于 null 的唯一合适时间,好像它到达了开关中的那个点,这意味着应该播放相应的动画。这就是我无法更改它并使我的代码更高效的原因。我正在研究使用 Ternar 分配,因为它可能是我正在寻找的解决方法。
  • Game.goingUp = (toy
  • @UnAlpha 我现在看到了 P1UAnim_mc 和其他人背后的逻辑,这就是我所说的不可读。你的代码非常丰富,以至于小细节会从一个人的感知中溜走。
猜你喜欢
  • 1970-01-01
  • 2016-12-31
  • 1970-01-01
  • 1970-01-01
  • 2016-10-15
  • 2023-02-11
  • 2013-05-16
  • 1970-01-01
  • 2020-12-07
相关资源
最近更新 更多