0

I've been getting into monogame recently and I came across a problem.

I've loaded a player sprite and I've got movement working, but what I want to do is change the sprite when I press right/left. I want the player ship to lean left/right according to the key press.

This is what my current code looks like, as you can see, I've attempted to achieve the desired effect with if statement but I wasn't successful.

    class Player
    {
        public Texture2D PlayerTexture; //Animation representing the layer
        public Texture2D moveLeft;
        public Texture2D moveRight;
        public Vector2 Position;    //Position of the Player relative to the upper left side of the screen
        public bool Active; //State of the player
        public int Health;  //Amount of hit points the player has
        public int Width { get { return PlayerTexture.Width; } }    //Get width of the player ship
        public int Height { get { return PlayerTexture.Height; } }  //Get height of the player ship

        //Keyboard states used to determine key presses
        KeyboardState currentKeyboardState;
        KeyboardState previousKeyboardState;

        //Mouse states used to track Mouse button press
        MouseState currentMouseState;
        MouseState previousMouseState;

        float playerMoveSpeed;

        //Player moving left or right
        bool movingLeft;
        bool movingRight;

        public void Initialize(Texture2D texture, Vector2 position)
        {
            PlayerTexture = texture;
            Position = position;    //Set the starting position of the player around the middle of the screen and to the back
            Active = true;  //Set the player to be active
            Health = 100;   //Set the player health

            //Set a constant player move speed
            playerMoveSpeed = 8.0f;
        }

        public void PmovingLeft(Texture2D pmoveleft, Vector2 position)
        {
            moveLeft = pmoveleft;
            Position = position;
        }

        public void PmovingRight(Texture2D pmoveright, Vector2 position)
        {
            moveRight = pmoveright;
            Position = position;
        }

        public void Update()
        {
            //Save the previous state of the keyboard so we can determine single key presses
            previousKeyboardState = currentKeyboardState;

            //Read the current state of the keyboard and store it
            currentKeyboardState = Keyboard.GetState();
        }

        public void UpdatePlayer(GameTime gameTime)
        {
            //Keyboard controls

            if (currentKeyboardState.IsKeyDown(Keys.Left))
            {
                Position.X -= playerMoveSpeed;
                movingLeft = true;
            }
            else { movingLeft = false; }
            if (currentKeyboardState.IsKeyDown(Keys.Right))
            {
                Position.X += playerMoveSpeed;
                movingRight = true;
            }
            else { movingRight = false; }
            if (currentKeyboardState.IsKeyDown(Keys.Up))
            {
                Position.Y -= playerMoveSpeed;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Down))
            {
                Position.Y += playerMoveSpeed;
            }

            //Make sure that the player does not go out of bounds
            Position.X = MathHelper.Clamp(Position.X, 0, Game1.screenWidth - Width);
            Position.Y = MathHelper.Clamp(Position.Y, 0, Game1.screenHeight - Height);
        }

        public void Draw(SpriteBatch spriteBatch)
        {


            if (movingLeft)
            {
                spriteBatch.Draw(moveLeft, Position, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
            }
            else { spriteBatch.Draw(PlayerTexture, Position, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f); }
            if (movingRight)
            {
                spriteBatch.Draw(moveRight, Position, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
            }
            else { spriteBatch.Draw(PlayerTexture, Position, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f); }
            }         
        }

What this code does is move the player left/right but the player sprite does not change correctly. If anyone can help me resolve this issue, it would be fantastic. Thanks in advance!

Game1.cs (main game class) can be provided upon request

user2572329
  • 35
  • 1
  • 8
  • What happens exactly when you press left / right? Can you also provide the code where you reference the Player class? – GMich Oct 22 '15 at 11:18

1 Answers1

2

Instead of dealing with all of these bools, what I would do is make an Enum.

So you could have an Enum SpriteState with different states and then a variable to hold the currentSpriteState

 Enum SpriteState
 {
   Left,
   Right,
   Straight
 }

 SpriteState currentSpriteState = SpriteState.Straight;

An example of changing states

  //generic condition and state change
if (condition) 
{
 currentSpriteState = SpriteState.Left;
}
else if(condition)
{
 currentSpriteState = SpriteState.Right;
}
else currentSpriteState = SpriteState.Straight;

Then have a switch case in the Draw() based on the currentSpriteState

 switch(currentSpriteState)
{
  case SpriteState.Left:
   spriteBatch.Draw(moveLeft, Position, null, Color.White, 0f,
   Vector2.Zero, 1f, SpriteEffects.None, 0f);
  break;

  case SpriteState.Right:
   spriteBatch.Draw(moveRight, Position, null, Color.White, 0f,
   Vector2.Zero, 1f, SpriteEffects.None, 0f);
  break;

  case SpriteState.Straight:
   spriteBatch.Draw(PlayerTexture, Position, null, Color.White, 0f,
   Vector2.Zero, 1f, SpriteEffects.None, 0f);
  break;
}

Bools could technically work, but this is much cleaner and easy to read and might just solve your problem.

jacobj
  • 21
  • 2