0

I am trying to achieve a very basic 2D game right now where an enemy is on the screen and it bounces back and forth between set points (50 and 500) kind of like a space invaders sort of thing. My issue is I can only get it to go right, but then not come back towards the left and repeat.

I was messing around with coding it myself before bothering to look into it and actually figure it out but I thought I had something that would work, but well it doesn't, my issue is I don' get why. My code is supposed to work like a switch, two if statements within the Update loop, one comes on the other goes off, one moves it right the other moves it left, I thought that was fine but it doesn't do that. It moves it right just fine but then the left part just doesn't work.

So, why does the following code not work?

namespace _2D_game_num1
{
    class Enemy
    {
        int health;
        Vector2 enemy_location = new Vector2(50, 50);
        Vector2 enemy_speed = new Vector2(1, 1);
        Player player = new Player("dummy");

        public Enemy()
        {
            health = 100;
        }

        public void UpdateLocation()
        {
            //Vector2 player_pos = player.GetLocation();
            //if (player_pos.X < 200)
            // Using the players location to figure out where the enemy should move
            bool right = true;
            bool left = false;
            if (right)
            {
                enemy_location.X += enemy_speed.X;
                if (enemy_location.X == 500)
                {
                    right = false;
                    left = true;
                }
            }

            if (left)
            {
                enemy_location.X -= enemy_speed.X;
                if (enemy_location.X == 50)
                {
                    right = true;
                    left = false;
                }
            }

        }

        public Vector2 GetLocation()
        {
            return enemy_location;
        }


    }
}

And then in the main Game class I have it so enemy1.UpdateLocation(); is within the Update section correctly (along with my players movement which works fine).

Machavity
  • 30,841
  • 27
  • 92
  • 100
jayelj
  • 47
  • 6
  • You don't need `right` and `left` variables. Simplify things by just having a 'right` variable. If it's true, you're moving right. If it's false, you're moving left. Also, be careful because vectors are based on floats which you should avoid testing for equality (same holds true for doubles). You're better off using `>= 500` and `<= 50` to avoid any "close but not quite 500 or 50" errors. – itsme86 Sep 06 '16 at 23:09
  • One other problem, you need to make that single `right` variable I was talking about a class member, not a local variable to `UpdateLocation()`. Since it's local to that method, it's not retaining its value. Every time you call `UpdateLocation()` right now, it's resetting `right` to `true` and `left` to `false`. That's probably the main problem you're having. – itsme86 Sep 06 '16 at 23:15

1 Answers1

1

Try this:

    public void UpdateLocation()
    {
            enemy_location.X += enemy_speed.X;

            if (enemy_location.X <= 50 || enemy_location.X >= 500)
            {
                enemy_speed.X = new Vector2(-enemy_speed.X, enemy_speed.Y);
            }
    }

What we are doing here is moving the enemy based on it's current speed. Then if the location is on the left or right of the screen, change direction.

Sometimes it pays to keep things simple. Get rid of the left and right flags, as they are just confusing things. When you're dealing with Update methods you're typically changing the state of something. The way you had it before, the left and right state was getting reset every time UpdateLocation is called.

Btw, you might want to consider passing in GameTime to your Update methods. Typically, when you're moving things around in real-time you'll want to multiply movement by some kind of deltaTime to keep things smooth on all devices. You're probably getting away with it because by default it'll be a fixed frame rate, but that may not always be the case, so it's a good habit to get into.

craftworkgames
  • 9,437
  • 4
  • 41
  • 52