For some reason, the image does not load. Can someone check my code? Here’s the pastebin link for the main file only:
thanks in advance for anyone who can help.
For some reason, the image does not load. Can someone check my code? Here’s the pastebin link for the main file only:
thanks in advance for anyone who can help.
Hey @Voxelazy, welcome to the forums.
What is the exact exception message youre getting? Im going to make an assumption that its a file not found exception, but want to be sure before answering.
Nope, it actually finds the file. No error messages whatsoever. But, the image does not get rendered on the window screen. I don’t know why, but I think it would help if you check my code from the Pastebin link. If you want, I can also give the source code of my Sprite class I guess.
Yea if you can give the player and sprite class so i can follow the code that would be helpful.
I assumed file not found originally because your code loaded images from the Content/
directory, and a common mistake is that people dont set the files to copy to output on build when not using the pipeline.
Here are the files for the Sprite and Player class:
The SpriteBatch.Draw
overload you are using is this one
SpriteBatch.Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color)
You’re supplying a sourceRectangle
parameter value where the Rectangle.X
and Rectangle.Y
values are based on the Sprite
position, and the Rectangle.Width
and Rectangle.Height
values are based on what is passed during constructor. The Sprite
position values change based on the player movement.
This is not what sourceRectangle
is for. The sourceRectanlge
parameter is where you supply a rectangular area within the Texture2D
to draw. For instance, if you had a sprite sheet that contained 32x32 sprites, and you wanted to use that single sprite sheet to draw the second sprite, you would supply a sourceRectangle
of (32, 0, 32, 32)
.
tl;dr You’re supplying a sourceRectangle
value, but it looks like your use of it is either misunderstood or incorrect. Change the SpriteBatch.Draw
method in Sprite
to just
spriteBatch.Draw(texture, position, color);
But now I can’t even resize the sprite.
If you need to resize, use the other SpriteBatch.Draw
overload where you provide a float
for the scale or the additional one that you can provide Vector2
for independent scaling on the x- and y-axies
Nevermind, I already found a solution:
public void Draw(SpriteBatch spriteBatch, Color color) {
Vector2 scale = new Vector2(width / (float)texture!.Width, height / (float)texture!.Height);
spriteBatch.Draw(texture, position, null, color, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f);
}
You can also use the overload with the destinationRectangle
which I think more closely aligns to what you are trying to do. With this, you define a rectangle where the Rectangle.X
and Rectangle.Y
values are the top-left position to draw the texture, and the Rectangle.Width
and Rectangle.Height
are used to define the size of the rectangle to fill (or stretch/shrink) the image within.
So in your example it would be
spriteBatch.Draw(texture new Rectangle((int)position.X, (int)position.Y, width, height), Color.White);