2014-04-25
Introduce a new way to add sprite animations to XNA game class.
here are the code that need for it.
public class Fire
{
Vector2 sposition;
Rectangle spriteAnimateRect;
Texture2D spriteCoin;
int currentFrame = 0;
int frameX = 0;
int frameY = 0;
float timer;
int interval = 100;
public Fire(ContentManager content,Vector2 position)
{
spriteCoin = content.Load<Texture2D>("fire_explosion");
sposition = position;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(spriteCoin, sposition,spriteAnimateRect, Color.White);
}
public void Update(GameTime gameTime)
{
spriteAnimateRect = new Rectangle(currentFrame + frameX, frameY, 120, 140);
Animate(gameTime);
}
public void Animate(GameTime gameTime)
{
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 2;
if (timer > interval)
{
timer = 0;
currentFrame += 120;
if (currentFrame >1725)
{
currentFrame = 0;
}
}
}