2

我有 19 张图片,它们是我的播放器的动画帧

在下面,我创建了纹理数组作为青蛙,这是我的播放器。有 19 个图像。如何为它们设置动画。

公共类纹理{公共静态Texture2D mBackground;公共静态Texture2D mBackgroundOne;公共静态Texture2D mBackgroundTwo;公共静态Texture2D GrassUp;公共静态Texture2D草地;
公共静态 Texture2D[] 青蛙 = 新 Texture2D[19]; 公共静态 Texture2D[] frogdie = new Texture2D[4];

    public static Vector2 position;

    public static void Load()
    {
        mBackground = GamePage.contentManager.Load<Texture2D>("layer_11");
        mBackgroundOne = GamePage.contentManager.Load<Texture2D>("layer_11");
        mBackgroundTwo = GamePage.contentManager.Load<Texture2D>("layer_11");
        grassUp = GamePage.contentManager.Load<Texture2D>("layer_11");
        grassDown = GamePage.contentManager.Load<Texture2D>("layer_11");


        frog[0] = GamePage.contentManager.Load<Texture2D>("player/maindak_normal1");
        frog[1] = GamePage.contentManager.Load<Texture2D>("player/maindak_normal2");
        frog[2] = GamePage.contentManager.Load<Texture2D>("player/maindak_normal3");
        frog[3] = GamePage.contentManager.Load<Texture2D>("player/maindak_normal4");
        frog[4] = GamePage.contentManager.Load<Texture2D>("player/maindak_normal5");
        frog[5] = GamePage.contentManager.Load<Texture2D>("player/maindak_normal6");
        frog[6] = GamePage.contentManager.Load<Texture2D>("player/maindak_normal7");
        frog[7] = GamePage.contentManager.Load<Texture2D>("player/maindak_normal8");
        frog[8] = GamePage.contentManager.Load<Texture2D>("player/maindak_normal9");
        frog[9] = GamePage.contentManager.Load<Texture2D>("player/maindak_normal10");
        frog[10] = GamePage.contentManager.Load<Texture2D>("player/maindak_normal11");
        frog[11] = GamePage.contentManager.Load<Texture2D>("player/maindak_normal12");
        frog[12] = GamePage.contentManager.Load<Texture2D>("player/maindak_normal13");
        frog[13] = GamePage.contentManager.Load<Texture2D>("player/maindak_normal14");
        frog[14] = GamePage.contentManager.Load<Texture2D>("player/maindak_normal15");
        frog[15] = GamePage.contentManager.Load<Texture2D>("player/maindak_normal16");
        frog[16] = GamePage.contentManager.Load<Texture2D>("player/maindak_normal17");
        frog[17] = GamePage.contentManager.Load<Texture2D>("player/maindak_normal18");
        frog[18] = GamePage.contentManager.Load<Texture2D>("player/maindak_normal19");

    }

    public  static void draw(SpriteBatch sprite)
    {
        for (int i = 0; i <= 18; i++)
        {
            sprite.Draw(frog[i],position= new Vector2(100, 100), Color.White);

        }

    }
4

1 回答 1

5

保持当前结构,您可以通过以下方式为纹理设置动画:

//timer
private const float TIME_BETWEEN_FRAME = 0.1f;
private float timer = TIME_BETWEEN_FRAME;

//frame sequence
private int currentFrame = 0;

public void Update(GameTime gametime)
{
    float elapsed = (float)gametime.ElapsedGameTime.TotalSeconds;

    timer -= elapsed;       //subtract elapsed time from timer
    if (timer <= 0)         //if our timer is elapsed
    {
        currentFrame++;     //next frame
        if (currentFrame >= frog.Count)
            currentFrame = 0;           //If we reach last frame, reset to loop
        timer = TIME_BETWEEN_FRAME;     //reset timer
    }
}

public  void draw(SpriteBatch spriteBatch)
{
    spriteBatch.Draw(frog[currentFrame], position, Color.White);
}

这将起作用,但是,如果您想朝着正确的方向更进一步,您应该研究SpriteSheets,它将为您提供一种更简单的方法来管理所有动画。

于 2013-10-08T14:40:37.357 回答