Time for action – animation and drawing properties
Add public properties to allow access to the Sprite class' members:
public int Frame { get { return currentFrame; } set { currentFrame = (int)MathHelper.Clamp(value, 0, frames.Count - 1); } } public float FrameTime { get { return frameTime; } set { frameTime = MathHelper.Max(0, value); } } public Rectangle Source { get { return frames[currentFrame]; } } public Rectangle Destination { get { return new Rectangle( (int)location.X, (int)location.Y, frameWidth, frameHeight); } } public Vector2 Center { get { return location + new Vector2(frameWidth / 2, frameHeight / 2); } }
What just happened?
The set portion of the Frame
property uses MathHelper.Clamp()
to ensure that when it is set, the value stored in currentFrame
is valid for the frames
list of Rectangles. This will prevent, for example, setting the frame...