Positioning a sprite in XNA: Use ClientBounds or BackBuffer?
Posted
by
Martin Andersson
on Game Development
See other posts from Game Development
or by Martin Andersson
Published on 2012-06-16T23:57:41Z
Indexed on
2012/06/17
3:25 UTC
Read the original article
Hit count: 342
I'm reading a book called "Learning XNA 4.0" written by Aaron Reed. Throughout most of the chapters, whenever he calculates the position of a sprite to use in his call to SpriteBatch.Draw
, he uses Window.ClientBounds.Width
and Window.ClientBounds.Height
. But then all of a sudden, on page 108, he uses PresentationParameters.BackBufferWidth
and PresentationParameters.BackBufferHeight
instead.
I think I understand what the Back Buffer and the Client Bounds are and the difference between those two (or perhaps not?). But I'm mighty confused about when I should use one or the other when it comes to positioning sprites. The author uses for the most part Client Bounds both for checking whenever a moving sprite is of the screen and to find a spawn point for new sprites. However, he seems to make two exceptions from this pattern in his book. The first time is when he wants some animated sprites to "move in" and cross the screen from one side to another (page 108 as mentioned). The second and last time is when he positions a texture to work as a button in the lower right corner of a Windows Phone 7 screen (page 379).
Anyone got an idea?
I shall provide some context if it is of any help. Here's how he usually calls SpriteBatch.Draw
(code example from where he positions a sprite in the middle of the screen [page 35]):
spriteBatch.Draw(texture,
new Vector2(
(Window.ClientBounds.Width / 2) - (texture.Width / 2),
(Window.ClientBounds.Height / 2) - (texture.Height / 2)),
null,
Color.White,
0,
Vector2.Zero,
1,
SpriteEffects.None,
0);
And here is the first case of four possible in a switch statement that will set the position of soon to be spawned moving sprites, this position will later be used in the SpriteBatch.Draw
call (page 108):
// Randomly choose which side of the screen to place enemy,
// then randomly create a position along that side of the screen
// and randomly choose a speed for the enemy
switch (((Game1)Game).rnd.Next(4))
{
case 0: // LEFT to RIGHT
position = new Vector2(
-frameSize.X, ((Game1)Game).rnd.Next(0,
Game.GraphicsDevice.PresentationParameters.BackBufferHeight
- frameSize.Y));
speed = new Vector2(((Game1)Game).rnd.Next(
enemyMinSpeed,
enemyMaxSpeed), 0);
break;
© Game Development or respective owner