gpt4 book ai didi

xna - 如何在 xna 游戏中制作差异图像动画?

转载 作者:行者123 更新时间:2023-12-02 21:47:39 30 4
gpt4 key购买 nike

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

在下面,我创建了 Frog 纹理数组,这是我的播放器。并且有 19 个图像。如何对它们进行动画处理。

公共(public)类纹理 { 公共(public)静态Texture2D mBackground; 公共(public)静态Texture2D mBackgroundOne; 公共(public)静态Texture2D mBackgroundTwo; 公共(public)静态Texture2D草地向上; 公共(public)静态Texture2DgrasDown;
公共(public)静态Texture2D [] Frog =新Texture2D [19]; 公共(public)静态Texture2D [] frogdie =新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);

}

}

最佳答案

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

//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 ,这将为您提供一种更简单的方法来管理所有动画。

关于xna - 如何在 xna 游戏中制作差异图像动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19249476/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com