Time for action – drawing the Sprite
Add the
Draw()
method to the Sprite class:public virtual void Draw(SpriteBatch spriteBatch) { spriteBatch.Draw( Texture, Center, Source, tintColor, rotation, new Vector2(frameWidth / 2, frameHeight / 2), 1.0f, SpriteEffects.None, 0.0f); }
What just happened?
The Draw()
method consists of a single call to the SpriteBatch.Draw()
method, using an overload of the method that allows for rotation and scaling.
Because we are specifying a rotation, we need to identify a center point for the rotation to be based around (new Vector2(frameWidth / 2, frameHeight / 2
). Instead of specifying a destination rectangle, we specify a vector that points to the center of the area the object will occupy on the screen (the Center
property of the Sprite class).