除非在

时间:2017-03-05 22:11:51

标签: actionscript-3 flash actionscript air cs3

我在游戏中运行的方式是检测到你点击了一个Movieclip的运行按钮,然后它设置了增加的walkpeeds。如果抬起手指,或将其从按钮上移开,它会将其恢复为默认的速度梯度。

所以,问题是运行按钮仅在定向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变量变为true,这意味着玩家正在移动。 玩家进入的方向的变量也将改变(如果他向左移动,则goLeft = true)

如果玩家遇到某些东西,或者让我们在DPAD上指示方向,那么inMotion就是假的。

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

我按左DPAD

  

inMotion = true,goingLeft = true

如果舞台上没有左侧动画,请将其添加到舞台上。

  
    

左侧动画检测到变量会相应地响应它们:         inMotion&& goingLeft             向左移动方向         !inMotion&& !goingLeft             然后闲着,不要动画         inMotion&& !goingLeft             在另一个方向移动,删除动画

  

我向右按DPAD 遵循上面提到的相同周期

这确保在正确的时间播放正确的动画,并且此代码可能比 它必须是,但这实际上显示了我在代码中所知道的限制。

1 个答案:

答案 0 :(得分:0)

只要看到2个看起来相似的代码块,就知道代码是错误的:https://en.wikipedia.org/wiki/Duplicate_code。公式 less code = better 始终是正确的。

空代码块会降低可读性:

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

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

您还可以将多个条件叠加到一个 if 的情况下,逻辑&&逻辑 ||除非它变得不可读:

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

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

如果你的目标是分配一个变量,而不是如果的攻击,你就可以使用一个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布局。您在库中设计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(...);
        }
    }
}