Time for action – animation and drawing properties

  1. 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 ...

Get XNA 4.0 Game Development by Example Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.