C# 更改XNA中的精灵图像

C# 更改XNA中的精灵图像,c#,animation,xna,sprite,C#,Animation,Xna,Sprite,这是我游戏中精灵移动的代码。我在网上看了一些教程和课程来帮助我解决这个问题。我需要知道如何在每次按下某个键时改变角色精灵。(例如,左键:左向精灵,右键:右向精灵)我在网上看过了,但我;我不知道如何将它集成到我的代码中。我相信我可以用“MageChar”类中的枚举列表来更改精灵,但我不确定如何开始。感谢您的帮助 游戏课 namespace RPG { public class MageChar : charMovement { //variable where sp

这是我游戏中精灵移动的代码。我在网上看了一些教程和课程来帮助我解决这个问题。我需要知道如何在每次按下某个键时改变角色精灵。(例如,左键:左向精灵,右键:右向精灵)我在网上看过了,但我;我不知道如何将它集成到我的代码中。我相信我可以用“MageChar”类中的枚举列表来更改精灵,但我不确定如何开始。感谢您的帮助

游戏课

namespace RPG
{
    public class MageChar : charMovement
    {
        //variable where sprite file name is stored
        const string MageAssetName = "MageChar";
        //starting x position
        const int StartPositionX = 0;
        //starting y position
        const int StartPositionY = 0;
        //speed that the sprite will move on screen
        const int MageSpeed = 160;
        //move sprite 1 up/down when the arrow key is pressed
        const int MoveUp = 1;
        const int MoveDown = -1;
        const int MoveLeft = -1;
        const int MoveRight = 1;

        //used to store the current state of the sprite
        enum State
        {
            Walking
        }
        //set to current state of the sprite. initally set to walking
        State CurrentState = State.Walking;

        //stores direction of sprite
        Vector2 Direction = Vector2.Zero;

        //stores speed of sprite
        Vector2 Speed = Vector2.Zero;

        //stores previous state of keyboard
        KeyboardState PreviousKeyboardState;

        public void LoadContent(ContentManager theContentManager)
        {
            //sets position to the top left corner of the screen
            Position = new Vector2(StartPositionX, StartPositionY);

            base.LoadContent(theContentManager, MageAssetName);
        }

        //checks state of the keyboard
        private void UpdateMovement(KeyboardState aCurrentKeyboardState)
        {
            //run if the sprite is walking
            if (CurrentState == State.Walking)
            {
                //sets direction and speed to zero
                Speed = Vector2.Zero;
                Direction = Vector2.Zero;

                //if left key is pressed, move left
                if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true)
                {
                    //speed of sprite movement
                    Speed.X = MageSpeed;
                    //moves the sprite left
                    Direction.X = MoveLeft;
                }
                //if right key is pressed, move right
                else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) == true)
                {
                    //speed of sprite movement
                    Speed.X = MageSpeed;
                    //moves the sprite right
                    Direction.X = MoveRight;
                }
                //if up key is pressed, move up
                if (aCurrentKeyboardState.IsKeyDown(Keys.Up) == true)
                {
                    //speed of sprite movement
                    Speed.Y = MageSpeed;
                    //moves sprite up
                    Direction.Y = MoveUp;
                }
                //if down key is pressed, move down
                else if (aCurrentKeyboardState.IsKeyDown(Keys.Down) == true)
                {
                    //speed of sprite movement
                    Speed.Y = MageSpeed;
                    //moves sprite down
                    Direction.Y = MoveDown;
                }
            }
        }

        public void Update(GameTime theGameTime)
        {
            //obtains current state of the keyboard
            KeyboardState aCurrentKeyboardState = Keyboard.GetState();

            //calls in UpdateMovement and passes in current keyboard state
            UpdateMovement(aCurrentKeyboardState);

            //set previous state to current state
            PreviousKeyboardState = aCurrentKeyboardState;

            //call update method of the charMovement class
            base.Update(theGameTime, Speed, Direction);
        }


    }
}
炭疽运动

namespace RPG
{
    public class charMovement
    {
        //The asset name for the Sprite's Texture
        public string charSprite;

        //The Size of the Sprite (with scale applied)
        public Rectangle Size;

        //The amount to increase/decrease the size of the original sprite. 
        private float mScale = 1.0f;
            //The current position of the Sprite
            public Vector2 Position = new Vector2(0,0);

            //The texture object used when drawing the sprite
            private Texture2D charTexture;


        //Load the texture for the sprite using the Content Pipeline
        public void LoadContent(ContentManager theContentManager, string theCharSprite)
        {
            //loads the image of the sprite
            charTexture = theContentManager.Load<Texture2D>("charSprite");

            charSprite = theCharSprite;
            //creates a new rectangle the size of the sprite
            Size = new Rectangle(0, 0, (int)(charTexture.Width * mScale), (int)(charTexture.Height * mScale));
        }

        //Update the Sprite and change it's position based on the set speed, direction and elapsed time.
        public void Update(GameTime theGameTime, Vector2 theSpeed, Vector2 theDirection)
        {
            Position += theDirection * theSpeed * (float)theGameTime.ElapsedGameTime.TotalSeconds;
        }

        //Draw the sprite to the screen
        public void Draw(SpriteBatch theSpriteBatch)
        {       
            //draw the sprite to the screen inside a rectangle     
            theSpriteBatch.Draw(charTexture, Position, 
                new Rectangle(0, 0, charTexture.Width, charTexture.Height), 
                Color.White, 0.0f, Vector2.Zero, mScale, SpriteEffects.None, 0);
        }
        }

    }

首先,您需要创建精灵纹理:将角色的所有图像放在一个纹理中(如下所示:)。假设现在你的角色有两种不同的状态——向左和向右,两种纹理都是64x64。所以我们有这样的东西:

-----------------
|       |       |
|  <--  |  -->  |
|       |       |
-----------------
您还可以使用方向或任何您想要的内容来代替状态。将来,您可能希望为整个行走动画和单个角色的其他动作提供不同的纹理

有关精灵动画的详细信息:

您还可以根据建议为不同的状态切换纹理。为此,只需加载两种不同的纹理,例如ChartTexture_Left和ChartTexture_Right,并根据当前状态在绘制方法中使用它们

要使CurrentState随用户箭头输入一起更改,请将CurrentState添加到UpdateMovement方法:

if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true)
{
    //speed of sprite movement
    Speed.X = MageSpeed;
    //moves the sprite left
    Direction.X = MoveLeft;
    CurrentState = State.Left; // set current state to left
 }
 else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) == true)
 {
     //speed of sprite movement
     Speed.X = MageSpeed;
     //moves the sprite right
     Direction.X = MoveRight;
     CurrentState = State.Right; // set current state to right
  }

您需要更改
图表结构。。因此,随着
绘图的下一次迭代的进行,
将出现。。它绘制了新的纹理。@Simon Whitehead——由于我对该语言不熟悉,因此可以解释您绘制下一个纹理的意思吗。您的游戏不断循环调用
Draw
方法。该方法使用
charTexture
变量来绘制角色。您只需要更改
charTexture
变量。当你这样做的时候。。
Draw
方法将继续使用
charTexture
进行绘制。。但是
charTexture
是一种新的纹理。。因此,一个新的精灵将出现。我将如何更改“ChartTexture”它以使图像随着用户键盘箭头输入而改变?
if (CurrentState == State.Left)
    theSpriteBatch.Draw(charTexture, Position, 
            new Rectangle(0, 0, 64, 64), // first frame 
            Color.White, 0.0f, Vector2.Zero, mScale, SpriteEffects.None, 0);
else if (CurrentState == State.Right)
    theSpriteBatch.Draw(charTexture, Position, 
            new Rectangle(64, 0, 64, 64), // second frame 
            Color.White, 0.0f, Vector2.Zero, mScale, SpriteEffects.None, 0);
if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true)
{
    //speed of sprite movement
    Speed.X = MageSpeed;
    //moves the sprite left
    Direction.X = MoveLeft;
    CurrentState = State.Left; // set current state to left
 }
 else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) == true)
 {
     //speed of sprite movement
     Speed.X = MageSpeed;
     //moves the sprite right
     Direction.X = MoveRight;
     CurrentState = State.Right; // set current state to right
  }