I have a problem regarding displaying sprite where the texture seams to be properly loaded (at least I don't receive any error messages even when removing the if
condition) but when I displaye it all I see is square in the color of Color
parameter while texture is stuck in the upper left corner visible only when square is hovered over it.
PNG texture was properly copied into Content folder using Content.mgcb.
public class Entity
{
public Vector2 Position { get; set; }
public Vector2 Diameters { get; set; }
public float Angle { get; set; }
public Texture2D EntityTexture { get; set; }
public int EntityLayer { get; set; }
public Entity(Vector2 position, Vector2 diameters, float angle, string spriteName, int entityTexture)
{
Position = position;
Diameters = diameters;
Angle = angle;
EntityTexture = Holder.content.Load<Texture2D>(spriteName);
EntityLayer = entityTexture;
}
public void DrawEntity()
{
if(EntityTexture != null)
{
Holder.spriteBatch.Draw(EntityTexture, Position, new Rectangle((int)(Position.X), (int)(Position.Y), (int)(Diameters.X), (int)(Diameters.Y)),
Color.White, Angle, new Vector2(EntityTexture.Bounds.Width/2, EntityTexture.Bounds.Height/2), Holder.scale, SpriteEffects.None, EntityLayer);
}
}
}
LoadContent()
in main class:
Holder.content = this.Content;
Holder.spriteBatch = new SpriteBatch(GraphicsDevice);
_player = new Player(new Vector2(300, 300), new Vector2(64, 64), 0.0f, "player", 1);
spriteBatch handling inside the Draw(GameTime gameTime)
in main class:
Holder.spriteBatch.Begin();
_player.DrawEntity();
Holder.spriteBatch.End();
base.Draw(gameTime);
Both Holder.spriteBatch
and Holder.content
are inside class Holder
:
namespace SpaceInvaderPlusPlus
{
class Holder
{
public static SpriteBatch spriteBatch;
public static ContentManager content;
public static float scale = 1.0f;
}
}
I tried giving the Holder.spriteBatch.Begin()
parameters SpriteSortMode.Deferred, BlendState.AlphaBlend
, chaning SpriteEffects.None
to new SpriteEffects()
as well as giving the texture image transparent background and changing Color.White
-> Color.Transparent
but all it did is changing the rendered square color to transparent
I think the problem stems from how SpriteBatch.Draw() is used (inside your entity class):
public void Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {...}
Here, position is meant to be the position in the game world (as you did correctly) but sourceRectangle's X and Y are meant to be the sprite/image/graphic position inside its source texture (if only one sprite/image/graphic is in the texture, it likely is 0, 0). Instead, you use Entity.Position again which probably isn't intended.
Width and Height of sourceRectangle are consequently the width and height of the sprite/image/graphic inside its source texture.
sourceRectangle is there to be used e.g. in combination with a texture atlas (multiple sprites in one large texture), or a sprite sheet (frames of a sprite animation all in one large texture)
Let me know if this helped!