1

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

EDIT:

Here's how I have done movement:

There is a movieclip thats binded to the coordinates of the player. This is what animations set their x and y coordinates to. If a player starts moving, then an inMotion variable becomes true, and this means the player is moving. A variable of the direction the player is going in also will change (if he's moving left goingLeft = true)

If the player hits something, or lets go of a direction on the DPAD, then inMotion is false.

This is done so that animations can be added to the stage at appropriate times, and be animated at appropriate times. For example:

I press left DPAD

inMotion = true, goingLeft = true

If the left animation is not on the stage, add it to the stage.

left animation detects variables are responds to them accordingly: inMotion && goingLeft move left direction !inMotion && !goingLeft were idle then, do not animate inMotion && !goingLeft were moving in another direction, remove the animation

I press right DPAD follows the same cycle mentioned above

This ensures the right animation is played at the correc times, and this code probably is longer than it needs to be, but this is honestly shows the limits to what I know in code.

UnAlpha
  • 127
  • 14
  • Last time I advised you to start approaching your code in an algorithmic way: http://stackoverflow.com/a/42421460/4687633. Please consider doing so, your code is full of unnecessarily duplicated chunks and finding something in it is a problem itself, let alone ruling the problem out. – Organis Mar 06 '17 at 06:40
  • Hi, I don't know what you mean by writing the code in an algorithmic way. – UnAlpha Mar 10 '17 at 01:41
  • Google > algorithmic thinking. Read a lot, learn a lot. Why? Because if you don't, you'll drown in the ocean of your own code. I already explained what's wrong with your code in the other thread, keeping pushing your way will get you nowhere. As you can see no one ventured to analyse your problem here, and the reason (might be the **only** reason) is your code that is 3-4 times as big as it really needs to be thus difficult to understand. – Organis Mar 10 '17 at 02:12
  • I updated the post with some information that might be useful, as I don't want to litter the comments with my statement. – UnAlpha Mar 12 '17 at 19:30
  • 1. You still have 4 pieces of absolutely identical code, one per each of switch block cases. 2. Is there a point to create UI layout with script instead of design? – Organis Mar 13 '17 at 02:14
  • About the 4 pieces of identicial code, that is what is detecting if the animation is present, and if it is not, adding it to the stage. This is only way I know of putting relevant animations on the stage; by putting the animation when it's certain that the player is going in a direction which is why I put it in each direction of the switch. By UI I will assume you mean the animations, I don't quite understand what you mean, but I will attempt to answer at my best. I add them through script because I want the code to be versatile, so the user can pick different characters and only the one – UnAlpha Mar 13 '17 at 05:39
  • in use gets placed. These animations will detect variables and act accordingly. This is why I do it through code. – UnAlpha Mar 13 '17 at 05:39

1 Answers1

0

As soon as you see 2 blocks of code that look similar, know that code is bad: https://en.wikipedia.org/wiki/Duplicate_code. The formula less code = better is always right.

Empty blocks of code reduce readability:

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

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

You can also stack several conditions into one if case with logical and && and logical or || unless it becomes unreadable:

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

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

If your aim is to assign a single variable, instead of barrage of ifs you can use a ternar operator. Again, unless readability drops:

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

So, your code with all of above applied:

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

You can additionally spare yourself of programming UI layout by designing UI in Flash IDE (Animate or CS6 or whichever you have there). You design MovieClip in Library so that it has all interfaces you need in right places. All UI elements you need to have assess to must be given instance names. Then you create a class for this 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(...);
        }
    }
}
Organis
  • 7,243
  • 2
  • 12
  • 14
  • Game.goingUp = (toy < 0); If the toy value is greater than 0, what does the value of this become? Is this using logic? – UnAlpha Mar 15 '17 at 20:08
  • More specifically about the logic thing, if its greater does it essentially ignore the value of toy? I assume if Game.goingUp is smaller than toy, Game.goingUp will equal toy. – UnAlpha Mar 15 '17 at 20:22
  • Also, I noticed that you put the if statements outside of the switch. This essentially breaks animations, because the P1UAnim_mc will always be the animation in use, this is why I put these statements in the switch up/down/left/right, because those are the only appropriate times to check if the animations are equal to null, as if it reaches that point in the switch it means that respective animation should be played. This is the reason I have not been able to change it and make my code more efficient. I'm looking into using the Ternar assignment as it probably is the fix I'm looking for. – UnAlpha Mar 15 '17 at 20:41
  • Game.goingUp = (toy < 0); means "if toy < 0 then goingUp = true else goingUp = false". Expression (toy < 0) is Boolean and return true/false with regard to toy value. – Organis Mar 15 '17 at 21:08
  • @UnAlpha I now see the logic behind P1UAnim_mc and others, that's what I call **unreadable**. You code is so aggressively abundant that minor details just slip through one's perception. – Organis Mar 15 '17 at 21:13