如何在xna游戏中制作不同的图像?

时间:2013-10-08 13:46:26

标签: xna

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

在下面我创建了一个纹理数组作为我的玩家青蛙。还有19个图像。它们可以动画它们。

公共类纹理     {         public static Texture2D mBackground;         public static Texture2D mBackgroundOne;         public static Texture2D mBackgroundTwo;         public static Texture2D grassUp;         public static Texture2D grassDown;
        public static Texture2D [] frog = new Texture2D [19];         public static 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);

        }

    }

1 个答案:

答案 0 :(得分: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,这将为你提供一种更简单的方法来管理你的所有动画。< / p>