如何为播放器添加动画?

How to add a animation to a Player?

我目前正在单人游戏中制作射击游戏。

我已经设法用我的 sprite 制作了一个不断循环的动画,但问题是我现在不知道如何将我的动画 sprite 插入到 myPlayer 中。

我不想要任何命令,只希望将一个不断循环的动画附加到 myPlayer。我是编程初学者,非常感谢您的帮助。

这个项目即将到期,希望得到一些帮助。

Game1:
        Texture2D myPlayerAnim;
        Rectangle myDestRect;
        Rectangle mySourceRect;
        Texture2D myEnemyAnim;
        Rectangle myEnemyDestRect;
        Rectangle myEnemySourceRect;
        float myElapsed;
        float myDelay = 100f;
        int myFrames = 0;

Initialize:
            myDestRect = new Rectangle(0, 0, 512, 512);
            myEnemyDestRect = new Rectangle(0, 0, 808, 608);

LoadContent: 

            myPlayerAnim = Content.Load<Texture2D>("SpriteSheetPlayerAnim");
            myEnemyAnim = Content.Load<Texture2D>("SpriteSheetEnemyAnim");

Update:    
            myElapsed += (float)aGameTime.ElapsedGameTime.TotalMilliseconds;

            if (myElapsed >= myDelay)
            {
                if (myFrames >= 3)
                {
                    myFrames = 0;
                }
                else
                {
                    myFrames++;
                }
                myElapsed = 0;
            }

            mySourceRect = new Rectangle(512 * myFrames, 0, 512, 512);
            myEnemySourceRect = new Rectangle(808 * myFrames, 0, 808, 608);

Draw: 

            mySpriteBatch.Draw(myPlayerAnim, myDestRect , mySourceRect, 
            Color.White);
            mySpriteBatch.Draw(myEnemyAnim, myEnemyDestRect, myEnemySourceRect, 
            Color.White);

Draw 目前只绘制动画,因为它没有附加到任何东西上。

目前 myPlayer 只附加了一个 png 而不是我的动画。

Draw: 
myPlayer = new Player(TextureLibrary.GetTexture("player"), myPlayerPos, 200, new Vector2(.3f, .3f), 0, Color.White, 1000, 1);

根据您提供的代码。我认为你只需要改变一些东西。

首先,直接使用 SpriteSheetPlayerAnim 作为播放器的纹理,而不是使用 player 纹理。

然后将播放器动画的UpdateDraw代码移动到Playerclass的UpdateDraw

他们会是这样的:

public class Player
{
    //Default constructor
    public Player()
    {
        //default values for your player//for example only
        //this.hp = 100;
        //this.mp = 20;
    }

    //Constructor
    public Player(GameTime myelapsedGameTime, Texture2D texPlayer, Vector2 playerPosition, int something, Vector2 vector2ForSomething, int intFST, Color playerColor, ...)
    {
        //initialise player's attributes

    }

    public override Update(GameTime gameTime)
    {
        if (myElapsed >= myDelay)
        {
            if (myFrames >= 3)
            {
                myFrames = 0;
            }
            else
            {
                myFrames++;
            }
            myElapsed = 0;
        }
    }

    public override Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(myPlayerAnim, myDestRect , mySourceRect, 
            Color.White);//for example
    }
}

然后在你的 Game1 class:

public class Game1 : Game
{
    Player player;
    GameTime myElapsed;

    public Game1()
    {
        //
    }

    protected override void LoadContent()
    {
        //Load your animated sprite here
        myPlayerAnim = Content.Load<Texture2D>("SpriteSheetPlayerAnim");

        player = new Player(myElapsed, myPlayerAnim, ...);//constructor with attributes here or whatever you want
    }

    protected override void Update(GameTime gameTime)
    {
        myElapsed += (float)aGameTime.ElapsedGameTime.TotalMilliseconds;
        player.Update(gameTime);
    }

    protected override void Draw(SpriteBatch spriteBatch)
    {
        player.Draw(spriteBatch);
    }
}

希望对您有所帮助!