gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:48:53 26 4
gpt4 key购买 nike

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

在下面,我创建了一个纹理数组作为我的播放器 Frog 。有 19 个图像。如何为它们制作动画。

公共(public)类纹理 { 公共(public)静态 Texture2D mBackground; 公共(public)静态 Texture2D mBackgroundOne; 公共(public)静态 Texture2D mBackgroundTwo; 公共(public)静态 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);

}

}

最佳答案

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

//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/

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