SpriteBatch.Draw Method
XNA Game Studio 4.0
Adds a sprite to a batch of sprites to be rendered.
| Name | Description |
|---|---|
| SpriteBatch.Draw (Texture2D, Rectangle, Color) | Adds a sprite to a batch of sprites for rendering using the specified texture, destination rectangle, and color. |
| SpriteBatch.Draw (Texture2D, Rectangle, Nullable<Rectangle>, Color) | Adds a sprite to a batch of sprites for rendering using the specified texture, destination rectangle, source rectangle, and color. |
| SpriteBatch.Draw (Texture2D, Rectangle, Nullable<Rectangle>, Color, Single, Vector2, SpriteEffects, Single) | Adds a sprite to a batch of sprites for rendering using the specified texture, destination rectangle, source rectangle, color, rotation, origin, effects and layer. |
| SpriteBatch.Draw (Texture2D, Vector2, Color) | Adds a sprite to a batch of sprites for rendering using the specified texture, position and color. |
| SpriteBatch.Draw (Texture2D, Vector2, Nullable<Rectangle>, Color) | Adds a sprite to a batch of sprites for rendering using the specified texture, position, source rectangle, and color. |
| SpriteBatch.Draw (Texture2D, Vector2, Nullable<Rectangle>, Color, Single, Vector2, Single, SpriteEffects, Single) | Adds a sprite to a batch of sprites for rendering using the specified texture, position, source rectangle, color, rotation, origin, scale, effects, and layer. |
| SpriteBatch.Draw (Texture2D, Vector2, Nullable<Rectangle>, Color, Single, Vector2, Vector2, SpriteEffects, Single) | Adds a sprite to a batch of sprites for rendering using the specified texture, position, source rectangle, color, rotation, origin, scale, effects and layer. |
Re: Interesting feature/bug
@Stephen: it's because you're using SpriteSortMode.FrontToBack. Use
SpriteSortMode.BackToFront instead. And in the future, post questions
on the MSDN forums or stackoverflow.com, not as comments to the
documentation...
- 6/18/2011
- BlueRaja
Interesting Feature/Bug
I'm new to XNA but have discovered something that seems somewhat odd with this draw method, or rather a combination of calls within a batch
for context: I was writing a quick learning experiment basically a simple Arcanoid clone
I'm using .Net 4.0 if this is relevant.
Relevant Code in the Render Function is as follows
spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend, SamplerState.LinearWrap,
DepthStencilState.Default, RasterizerState.CullNone);
// darw main ball
spriteBatch.Draw(mainball, spritePosition, Color.White);
// draw Top HUD background
Rectangle destRect = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, topHUD.Height);
spriteBatch.Draw(topHUD, Vector2.Zero, destRect, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None,0);
Vector2 ballpos = new Vector2();
// draw a small ball for each life, in the HUD area
for (int i = 0; i < Lives; i++)
{
ballpos.X = (smallBall.Width + 5) * i + 5;
ballpos.Y = 5;
spriteBatch.Draw(smallBall, ballpos, Color.White);
}
spriteBatch.End();
Now the interesting thing is this. 1 small ball comes up in front of the HUD bar and the rest behind it. Can anyone please explain the logic of this? n.b. I've tried extending the tile to a large enough basic image in case it was something to do with tiling and this makes no difference.
for context: I was writing a quick learning experiment basically a simple Arcanoid clone
I'm using .Net 4.0 if this is relevant.
Relevant Code in the Render Function is as follows
spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend, SamplerState.LinearWrap,
DepthStencilState.Default, RasterizerState.CullNone);
// darw main ball
spriteBatch.Draw(mainball, spritePosition, Color.White);
// draw Top HUD background
Rectangle destRect = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, topHUD.Height);
spriteBatch.Draw(topHUD, Vector2.Zero, destRect, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None,0);
Vector2 ballpos = new Vector2();
// draw a small ball for each life, in the HUD area
for (int i = 0; i < Lives; i++)
{
ballpos.X = (smallBall.Width + 5) * i + 5;
ballpos.Y = 5;
spriteBatch.Draw(smallBall, ballpos, Color.White);
}
spriteBatch.End();
Now the interesting thing is this. 1 small ball comes up in front of the HUD bar and the rest behind it. Can anyone please explain the logic of this? n.b. I've tried extending the tile to a large enough basic image in case it was something to do with tiling and this makes no difference.
- 4/6/2011
- Stephen Whipp