Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I was trying to develop a very simple ping pong game using XNA.

In my Draw() I am drawing the player paddles first then the ball as follows:

spriteBatch.Draw(myPlayer, myPlayer1Pos, null, Color.Aqua);
spriteBatch.Draw(myPlayer, myPlayer2Pos, null, Color.Aqua);
spriteBatch.Draw(myBall, myBallPos, Color.LawnGreen);

Now the problem is the ball is no longer displayed in the game window as shown below enter image description here

If on the other hand I draw the ball before the player paddle

spriteBatch.Draw(myBall, myBallPos, Color.LawnGreen);
spriteBatch.Draw(myPlayer, myPlayer1Pos, Color.Aqua);
spriteBatch.Draw(myPlayer, myPlayer2Pos, Color.Aqua);

then the right hand side paddle is no longer visible as shown below: enter image description here

Can anyone explain what is going wrong here?

edit: The complete Draw():

protected override void Draw(GameTime gameTime)
{
      GraphicsDevice.Clear(Color.CornflowerBlue);

      // TODO: Add your drawing code here
      spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);

      spriteBatch.Draw(myBall, myBallPos, null, Color.LawnGreen);

      spriteBatch.Draw(myPlayer, myPlayer1Pos, null, Color.Aqua);
      spriteBatch.Draw(myPlayer, myPlayer2Pos, null, Color.Aqua);

      spriteBatch.Draw(myBkgTexture, graphics.GraphicsDevice.Viewport.Bounds, Color.Blue);

      spriteBatch.End();

      base.Draw(gameTime);
}
share|improve this question
    
Can you post more info about the problem, please? –  Gustavo Maciel Mar 10 '12 at 18:55
    
Hey thanks, but can you tell what additional information about problem you want because this is all which I am drawing inside the Draw(). –  Gamer Mar 10 '12 at 19:00
    
Thats definitly a bit peculiar. What happens when you draw the ball with the same overloaded Draw as the paddles? (the nullable rectangle after the position) –  MintyAnt Mar 10 '12 at 19:02
    
There is no change. If I understand correctly null ensures that the sprite is drawn in entirely. Please correct me if i am wrong. –  Gamer Mar 10 '12 at 19:05
    
Dimensions: Player paddle is 14*60 and ball is 16*16 –  Gamer Mar 10 '12 at 19:08

2 Answers 2

up vote 5 down vote accepted

The first problem is that you're using SpriteSortMode.BackToFront which to put it simply, is not appropriate for this case because you're not even specifying the depth of your sprites. Just use the default mode instead which draws in the same order as your calls (just write spriteBatch.Begin() with no parameters). Also, it's strange that you're drawing the background after the other sprites. If it's the background it should come first otherwise it will occlude everything else.

Here's the corrected code, with some redundant parameters removed:

spriteBatch.Begin();
spriteBatch.Draw(myBkgTexture, graphics.GraphicsDevice.Viewport.Bounds, Color.Blue);
spriteBatch.Draw(myBall, myBallPos, Color.LawnGreen);
spriteBatch.Draw(myPlayer, myPlayer1Pos, Color.Aqua);
spriteBatch.Draw(myPlayer, myPlayer2Pos, Color.Aqua);
spriteBatch.End();
share|improve this answer
1  
Hi David, thanks. The Begin() with SpriteSortMode.BackToFront was generated by the Wizard and I never went to change it. But as mentioned by you using the default Begin() solves the issue. Thanks a lot :) –  Gamer Mar 11 '12 at 5:13

The issue seems to be with spriteBatch.Begin() which the wizard had generated with SpriteSortMode.BackToFront.

Using the default Begin() solves the issue.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.