Time for action – Sprite constructor

  1. Add a constructor to the Sprite class:
    public Sprite(
        Vector2 location,
        Texture2D texture,
        Rectangle initialFrame,
        Vector2 velocity)
    {
        this.location = location;
        Texture = texture;
        this.velocity = velocity;
    
        frames.Add(initialFrame);
        frameWidth = initialFrame.Width;
        frameHeight = initialFrame.Height;
    }

What just happened?

The constructor for the Sprite class directly sets the location, texture, and velocity members to the passed parameter values. It then sets the frameWidth and frameHeight variables by extracting that information from the Rectangle.

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.