【问题标题】:Change player sprite on key press在按键时更改播放器精灵
【发布时间】:2015-10-22 01:22:18
【问题描述】:

我最近开始玩单机游戏,但遇到了一个问题。

我已经加载了一个播放器精灵并且我已经开始运动了,但是我想要做的是在我按下右/左键时更改精灵。我希望玩家飞船根据按键向左/向右倾斜。

这是我当前代码的样子,如您所见,我尝试使用 if 语句达到预期的效果,但没有成功。

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

这段代码的作用是向左/向右移动玩家,但玩家精灵没有正确改变。如果有人能帮我解决这个问题,那就太好了。提前致谢!

Game1.cs(主游戏类)可根据要求提供

【问题讨论】:

  • 按左/右时会发生什么?能否提供引用 Player 类的代码?

标签: c# xna sprite monogame


【解决方案1】:

我不会处理所有这些布尔值,而是创建一个枚举。

所以你可以有一个具有不同状态的 Enum SpriteState,然后是一个变量来保存 currentSpriteState

 Enum SpriteState
 {
   Left,
   Right,
   Straight
 }

 SpriteState currentSpriteState = SpriteState.Straight;

改变状态的例子

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

然后根据currentSpriteState在Draw()中有一个switch case

 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 可以工作,但它更简洁易读,可能会解决您的问题。

【讨论】:

    猜你喜欢
    • 2019-05-01
    • 1970-01-01
    • 2016-09-02
    • 2018-11-18
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    相关资源
    最近更新 更多