Search code examples
c#visual-studioparametersdrawmonogame

Why can't I use a specific Draw override method from Monogames?


I'm using Monogames framework in Visual Studio 2019.

When I try to use SpriteBatch.Draw() I get 8 different overrides to select from. When I have enter the correct parameter for that override function, I get error messages as I'm trying to use another override with different parameters and I don't understand why.

Like this picture below enter image description here

Here I have entered the parameters correctly but I get error messages like so enter image description here

Why is Visual Studio or MonoGame framework not understanding what override function I want to use?

A side note on this is that I can use an override function with a lot of parameters but I don't want to use it cause of more stress on performance. And also it is obeselete.

enter image description here


Solution

  • When I have enter the correct parameter for that override function, I get error messages as I'm trying to use another override with different parameters and I don't understand why.

    Almost certainly, you've just got one of your types wrong. There's so many overloads of the Draw method, I can understand why this is such an easy mistake to make. I've made it myself a bunch of times.

    One way to figure out what's going wrong is to declare each parameter with it's type right above the method call so you can clearly see what's going on.

    Texture2D texture = _image;
    Rectangle destinationRectangle = dRectangle;
    Rectangle? sourceRectangle  = null;
    Color color = Color.White;
    float rotation = Orientation;
    Vector2 origin = Vector2.Zero;
    SpriteEffects effects = SpriteEffects.None;
    float layerDepth = 0;
    
    spriteBatch.Draw(texture, destinationRectangle, sourceRectangle, color, rotation, origin, effects, layerDepth);
    

    Once everything is working you can of course refactor the code to inline the parameters or whatever.

    A side note on this is that I can use an override function with a lot of parameters but I don't want to use it cause of more stress on performance.

    Also, be careful making assumptions about performance too early. Unless you've actually measured both ways and found a performance problem there's no reason to believe any of the Draw methods have a noticeable impact on performance.