How Would You Draw a RectangleF with Spritebatch.Draw?

946 Views Asked by At

I'm trying to create a top down game where you can move in 8 directions. I've implemented Vector2 in order to move in a normalised manner. I use a spritebatch system to draw with standard rectangles (source and destination) but soon changed to RectangleF as regular rectangles use integers.

The Spritebatch does not recognise the RectangleF and hence returns the error, "Cannot convert from 'System.Drawing.RectangleF' to 'Microsoft.Xna.Framework.Vector2'?". The Color also returns ambiguous and no matter 'System.Drawing' or 'Microsoft.Xna.Framework.Color', it always returns how it cannot be converted into 'Microsoft.Xna.Framework.Rectangle'.

In this code, a Vector2 (Position) is called in and used to draw the RectangleF's position. I then use the Spritebatch.Draw to draw a texture with the Source and Destination and it is always updated to give the implication of a moving character.

public static RectangleF destinationRectangle, sourceRectangle;

public void Draw(SpriteBatch spriteBatch)   
    {
        int width = 64;
        int height = 64;
        realPositionX = PlayerMovement.Position.X; 
        realPositionY = PlayerMovement.Position.Y;

        sourceRectangle = new RectangleF(currentFrame * width, row * height, width, height);
        destinationRectangle = new RectangleF(realPositionX, realPositionY, width, height);
        spriteBatch.Begin();
        spriteBatch.Draw(Player, destinationRectangle, sourceRectangle, Microsoft.Xna.Framework.Color.White); //Error Occurs Here
        spriteBatch.End();
    }

I'm confused as how I should approach this issue, is there a way of drawing a RectangleF through SpriteBatch or do I have to look at another method?

Thanks.

1

There are 1 best solutions below

2
Pavel Slesinger On

There is absolutely no reason to use RectangleF. Usually you use a pixel based coordinate system in your game and therefore the Rectangle Class is what you want to go for.

Other than that it is a better approach to store the players postion in a Vector2 and using the the appropriate overload:

SpriteBatch.Draw (Texture2D, Vector2, Color)