动画在 XNA 中不起作用
Animation won't work in XNA
我制作了 class 剪切大型 spritesheet 的部分并将其绘制在屏幕上。
我设法绘制了那部分场景,只是它不会动画。
public AnimatedSprite(Vector2 position, string texture)
{
sPostion = position;
sTexture = Asset_Manager.GetInstance().texture[texture];
}
/// <summary>
/// Adds an animation to the AnimatedSprite
/// </summary>
public void AddAnimation(int frames, int yPos, int xStartFrame, string name, int width, int height, Vector2 offset)
{
//Creates an array of rectangles which will be used when playing an animation
Rectangle[] Rectangles = new Rectangle[frames];
//Fills up the array of rectangles
for (int i = 0; i < frames; i++)
{
Rectangles[i] = new Rectangle((i + xStartFrame) * width, yPos, width, height);
}
sAnimations.Add(name, Rectangles);
sOffsets.Add(name, offset);
}
/// <summary>
/// Determines when we have to change frames
/// </summary>
/// <param name="GameTime">GameTime</param>
public virtual void Update(GameTime gameTime)
{
timeElapsed += gameTime.ElapsedGameTime.TotalSeconds;
if (timeElapsed > timeToUpdate)
{
timeElapsed -= timeToUpdate;
if (frameIndex < sAnimations[currentAnimation].Length - 1)
{
frameIndex++;
}
else //Restarts the animation
{
frameIndex = 0;
}
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(sTexture, sPostion, sAnimations[currentAnimation][frameIndex], Color.White);
}
public void PlayAnimation(string name)
{
if (currentAnimation != name)
{
currentAnimation = name;
}
}
}
}
动画Class 。
玩家Class
它不会循环播放动画。
public class Player
{
AnimatedSprite player;
public Player()
{
player = new AnimatedSprite(new Vector2(100,100),"Ships");
player.AddAnimation(2, 0, 1, "GreenPlane", 25, 26, new Vector2(0, 0));
player.PlayAnimation("GreenPlane");
}
public void Update(GameTime gameTime)
{
player.Update(gameTime);
}
public void Draw(SpriteBatch spriteBach)
{
player.Draw(spriteBach);
}
它绘制第一帧并且不会将第 1 帧更改为第 x 帧并循环返回,
5 天前问过,所以我认为您已经找到了解决方案..
如果不是:
你的代码对我来说很好。我将您的代码用于测试项目,它更新并呈现得很好。在您的代码片段中,您在播放器(AnimatedSprite 类型)上调用了 .Update(GameTime),但是您是否也在播放器实例(Player 类型)本身上调用了 .Update(GameTime)?
-
我用你链接的 类 和 'pure' xna 试过了 - 不是单声道
我制作了 class 剪切大型 spritesheet 的部分并将其绘制在屏幕上。 我设法绘制了那部分场景,只是它不会动画。
public AnimatedSprite(Vector2 position, string texture)
{
sPostion = position;
sTexture = Asset_Manager.GetInstance().texture[texture];
}
/// <summary>
/// Adds an animation to the AnimatedSprite
/// </summary>
public void AddAnimation(int frames, int yPos, int xStartFrame, string name, int width, int height, Vector2 offset)
{
//Creates an array of rectangles which will be used when playing an animation
Rectangle[] Rectangles = new Rectangle[frames];
//Fills up the array of rectangles
for (int i = 0; i < frames; i++)
{
Rectangles[i] = new Rectangle((i + xStartFrame) * width, yPos, width, height);
}
sAnimations.Add(name, Rectangles);
sOffsets.Add(name, offset);
}
/// <summary>
/// Determines when we have to change frames
/// </summary>
/// <param name="GameTime">GameTime</param>
public virtual void Update(GameTime gameTime)
{
timeElapsed += gameTime.ElapsedGameTime.TotalSeconds;
if (timeElapsed > timeToUpdate)
{
timeElapsed -= timeToUpdate;
if (frameIndex < sAnimations[currentAnimation].Length - 1)
{
frameIndex++;
}
else //Restarts the animation
{
frameIndex = 0;
}
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(sTexture, sPostion, sAnimations[currentAnimation][frameIndex], Color.White);
}
public void PlayAnimation(string name)
{
if (currentAnimation != name)
{
currentAnimation = name;
}
}
}
}
动画Class 。
玩家Class
它不会循环播放动画。
public class Player
{
AnimatedSprite player;
public Player()
{
player = new AnimatedSprite(new Vector2(100,100),"Ships");
player.AddAnimation(2, 0, 1, "GreenPlane", 25, 26, new Vector2(0, 0));
player.PlayAnimation("GreenPlane");
}
public void Update(GameTime gameTime)
{
player.Update(gameTime);
}
public void Draw(SpriteBatch spriteBach)
{
player.Draw(spriteBach);
}
它绘制第一帧并且不会将第 1 帧更改为第 x 帧并循环返回,
5 天前问过,所以我认为您已经找到了解决方案..
如果不是: 你的代码对我来说很好。我将您的代码用于测试项目,它更新并呈现得很好。在您的代码片段中,您在播放器(AnimatedSprite 类型)上调用了 .Update(GameTime),但是您是否也在播放器实例(Player 类型)本身上调用了 .Update(GameTime)?
-
我用你链接的 类 和 'pure' xna 试过了 - 不是单声道