2
\$\begingroup\$

I want to flip my sprite horizontally while drawing and I can't figure out how to use the correct parameters without either an error or a warning about the method being obsolete.

This is what I assume it would be:

sb.Draw(NumbersGame.Arrow, resolutionRightArrow, Color.White, 
SpriteEffects.FlipHorizontally);

Except it comes up for errors for the color (cannot convert from color to rectangle) and the SpriteEffects bit (cannot convert from sprite effects to rectangle).

What is the correct way to lay out these arguments?

P.S: When I provide all the default values (says it's obsolete):

sb.Draw(NumbersGame.Arrow, resolutionRightArrow, null, null, null, 0f, null, 
Color.White, SpriteEffects.FlipHorizontally);
\$\endgroup\$
3
  • \$\begingroup\$ You can use Ctrl + Shift + Space to view the possible overloads of the selected line. That way you'll know which parameter to use. There's also a possible button for it in the toolbar, saying "Display Parameter Info" \$\endgroup\$
    – Steven
    May 6, 2019 at 6:33
  • \$\begingroup\$ Check out the documentation for right call for the method Draw() it shows multiple overload options. docs.microsoft.com/en-us/previous-versions/windows/xna/… \$\endgroup\$
    – J-ho
    Oct 11, 2020 at 13:17
  • \$\begingroup\$ What is the overload you have used that did work, without the horisontal flipping? \$\endgroup\$ Sep 24, 2021 at 20:14

2 Answers 2

1
\$\begingroup\$

The obsolete function signatures have been removed.

There are only two complete Draw functions in monogame(all of the others fill in the missing parameters and call one of these two), one uses a destination Rectangle the other a Vector2 position.

The only difference between the two is in the way the scaling techniques are applied:

The destinationRectangle size is calculated against the source rectangle size. Resulting in two division operations per call.

The position variant takes a Vector2 position and a Vector2 scale parameter.

Either way don't forget to subtract the origin from the position to get the top-left point.

The Destination Rectangle Overload

public void Draw(Texture2D texture,
    Rectangle destinationRectangle,
    Rectangle? sourceRectangle,
    Color color,
    float rotation,
    Vector2 origin,
    SpriteEffects effects,
    float layerDepth)

texture - The Texture2D object to be drawn.

destinationRectangle - The drawing bounds on screen. Scales to the width and height of the given rectangle.The position of the rectangle is the top left - the origin parameter.

sourceRectangle - An optional region on the texture which will be rendered. If null - draws full texture.

color - A value multiplied to each of the final pixels; RGBA clamped to 0-1. This can only darken colors, or increase transparency.

rotation - A rotation of this sprite in radians.

origin - Center of the rotation. 0,0 by default. A

effects - FlipHorizontal or FlipVertical. Can be combined with |.

layerDepth - A depth of the layer of this sprite. Order that the sprite is drawn. Can be any value. This value is used as the Z coordinate for the drawn quad.

The Vector2 Position Overload

public void Draw (Texture2D texture,
                Vector2 position,
                Rectangle? sourceRectangle,
                Color color,
                float rotation,
                Vector2 origin,
                Vector2 scale,
                SpriteEffects effects,
                float layerDepth)

texture - The Texture2D object to be drawn.

position - The Top Left corner - the origin parameter.

sourceRectangle - An optional region on the texture which will be rendered. If null - draws full texture.

color - A value multiplied to each of the final pixels; RGBA clamped to 0-1. This can only darken colors, or increase transparency.

rotation - A rotation of this sprite in radians.

origin - Center of the rotation. 0,0 by default.

scale - The size scaling of the sourceRectangle size values.

effects - FlipHorizontal or FlipVertical. Can be combined with |.

layerDepth - A depth of the layer of this sprite. Order that the sprite is drawn. Can be any value. This value is used as the Z coordinate for the drawn quad.

\$\endgroup\$
0
\$\begingroup\$

Have you tried this overload?

public void Draw(Texture2D texture, Rectangle destinationRectangle, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth);
\$\endgroup\$
3
  • \$\begingroup\$ What value do I need to use for layerDepth? -100, -1, 0, 1, and 100 don't work. \$\endgroup\$
    – liaquore
    May 6, 2019 at 20:36
  • \$\begingroup\$ It can range from 0 to 1f, try 0.5f for example \$\endgroup\$
    – Silver
    May 7, 2019 at 11:25
  • 1
    \$\begingroup\$ This answer could benefit from an explanation as to why this particular overload solves OPs problem. \$\endgroup\$
    – bornander
    May 10, 2019 at 5:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .