Welcome to Darren Sim's Playpans Sign in | Join | Help

XNA AnimatedTexture Class

If you've been trying to try out the XNA Tutorial on the documentation and could not seem to find the AnimatedTexture class, here it is! It's not part of the class library.

public class AnimatedTexture
{

private int framecount;
private Texture2D myTexture;
private float TimePerFrame;
private int Frame;
private float TotalElapsed;
private bool Paused;
public float Rotation, Scale, Depth;
public Vector2 Origin;

public AnimatedTexture(Vector2 Origin, float Rotation, float Scale, float Depth)
{

this.Origin = Origin;
this.Rotation = Rotation;
this.Scale = Scale;
this.Depth = Depth;

}

public void Load(GraphicsDevice device, ContentManager content, string asset, int FrameCount, int FramesPerSec)
{

framecount = FrameCount;
myTexture = content.Load<
Texture2D>(asset);
TimePerFrame = (
float)1 / FramesPerSec;
Frame = 0;
TotalElapsed = 0;
Paused =
false;

}

// class AnimatedTexture
public void UpdateFrame(float elapsed)
{

if (Paused)
return;

TotalElapsed += elapsed;

if (TotalElapsed > TimePerFrame)
{

Frame++;

// Keep the Frame between 0 and the total frames, minus one.
Frame = Frame % framecount;
TotalElapsed -= TimePerFrame;

}

}

// class AnimatedTexture
public void DrawFrame(SpriteBatch Batch, Vector2 screenpos)
{

DrawFrame(Batch, Frame, screenpos);

}

public void DrawFrame(SpriteBatch Batch, int Frame, Vector2 screenpos)
{

int FrameWidth = myTexture.Width / framecount;
Rectangle sourcerect = new Rectangle(FrameWidth * Frame, 0,
FrameWidth, myTexture.Height);
Batch.Draw(myTexture, screenpos, sourcerect,
Color.White,
Rotation, Origin, Scale,
SpriteEffects.None, Depth);

}

public bool IsPaused
{

get { return Paused; }

}

public void Reset()
{

Frame = 0;
TotalElapsed = 0f;

}

public void Stop()
{

Pause();
Reset();

}

public void Play()
{

Paused = false;

}

public void Pause()
{

Paused = true;

}

}

Published Thursday, June 28, 2007 11:45 PM by darren.sim
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
required 
(required)