2 using System.Collections.Generic;
3 using Microsoft.Xna.Framework;
4 using Microsoft.Xna.Framework.Content;
5 using Microsoft.Xna.Framework.Graphics;
6 using Microsoft.Xna.Framework.Input;
10 public class AnimatedTexture
12 private int framecount;
13 private Texture2D myTexture;
14 private float TimePerFrame;
15 private int Frame, Row;
16 private float TotalElapsed;
17 private bool Paused, loop;
19 public float Rotation, Scale, Depth;
20 public Vector2 Origin;
22 //rotation is pretty much always zero, scale determines size, depth is 0.5f
23 public AnimatedTexture(Vector2 Origin, float Rotation, float Scale, float Depth)
26 this.Rotation = Rotation;
30 //asset is the file name w/o the extension, framecount is the # of frames per row, framespersec determines speed of the animation
31 //loop will determine if the animation repeats, row chooses which row of the animation to play starting with 0
32 public void Load(GraphicsDevice device, ContentManager content, string asset, int FrameCount, int FramesPerSec, bool Loop, int row)
34 framecount = FrameCount;
35 myTexture = content.Load<Texture2D>(asset);
36 TimePerFrame = (float)1 / FramesPerSec;
44 // class AnimatedTexture
45 public void UpdateFrame(TimeSpan timeSpan)
47 float elapsed = timeSpan.Milliseconds;
50 TotalElapsed += elapsed;
51 if (TotalElapsed > TimePerFrame)
54 // Keep the Frame between 0 and the total frames, minus one.
56 Frame = Frame % framecount;
58 Console.WriteLine("AHHH!");
59 TotalElapsed -= TimePerFrame;
61 //If loop is false and the current Frame is the last frame
62 //then stop playing the animation
63 if ((Frame == framecount - 1) && loop == false)
68 // class AnimatedTexture
69 public void DrawFrame(SpriteBatch Batch, Vector2 screenpos)
71 DrawFrame(Batch, Frame, screenpos);
73 public void DrawFrame(SpriteBatch Batch, int frame, Vector2 screenpos)
76 int FrameWidth = myTexture.Width / framecount;
77 Rectangle sourcerect = new Rectangle(FrameWidth * Frame, Row * 64,
79 Console.WriteLine(Frame);
80 Batch.Draw(myTexture, screenpos, sourcerect, Color.White,
81 Rotation, Origin, Scale, SpriteEffects.None, Depth);
86 get { return Paused; }