dimanche 5 mars 2017

Pressing run button does not increase speed unless pressed before

The way I've done the run in my game is it detects you clicked the run button which is a Movieclip, then it set the increased walkspeeds. If you lift your finger, or move it off the button, it reverts it back the default walkspeed is.

So, the problem is the run button only works when pressed prior to the directional DPAD.

How do I fix this?

My movement class

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;
            }
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire