3
\$\begingroup\$
for (int i = -5; i < 5; i++)
{
    Rectangle screenRectangle = new Rectangle(0, 0, screenWidth, screenHeight);
    spriteBatch.Draw(foregroundTexture, screenRectangle, Color.White);

    Rectangle backGroundRectangle = new Rectangle(x + screenWidth * i, 0, screenWidth, screenHeight);
    spriteBatch.Draw(backgroundTexture, backGroundRectangle, Color.White);

    spriteBatch.DrawString(font, "Variable i:  " + i, new Vector2(0, 0), Color.Brown);
}

I want to show on the screen when I'm running the game the counting of i like: -5 , -4 , -3...but not in a line, just like a timer. -5 then overwrite it and show -4 overwrite it and show -3 and so on...like seconds of a clock.

The problem is I'm getting a strange painting near the numbers like its repeating and painting something there all the time.

\$\endgroup\$
3
  • \$\begingroup\$ Do you call GraphicsDevice.Clear() anywhere? \$\endgroup\$ Commented Jul 8, 2011 at 10:11
  • \$\begingroup\$ I think you need to learn how loops work. \$\endgroup\$ Commented Jul 8, 2011 at 16:51
  • \$\begingroup\$ @AttackingHobo I would say OP needs to learn about variable scope first... \$\endgroup\$
    – Nate
    Commented Jul 8, 2011 at 21:51

1 Answer 1

8
\$\begingroup\$

This won't work because in your for loop, the command list is never actually executed, this happens only at the end of the Draw function in your Game class; this is effectively where the frame ends and is drawn to your screen.

So what you see happening is all your stuff being drawn on the same, single frame, which is what causes the garbage on-screen.

For it to work proplerly, your variable i must be outside of the Draw(GameTime gameTime) function, and incremented outside of the function also.

At the start of Draw() you must call clear and then call your function to draw your backgroud/foreground and text without the for loop.

For example :

Initialize() {
    i = -5;
}

Update(GameTime gameTime) {
    if(i < 5) ++i;

    base.Update(gameTime);
}

Draw(GameTime gameTime) {
    graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

    Rectangle screenRectangle = new Rectangle(0, 0, screenWidth, screenHeight);
    spriteBatch.Draw(foregroundTexture, screenRectangle, Color.White);

    Rectangle backGroundRectangle = new Rectangle(x + screenWidth * i, 0, screenWidth, screenHeight);
    spriteBatch.Draw(backgroundTexture, backGroundRectangle, Color.White);

    spriteBatch.DrawString(font, "Variable i:  " + i, new Vector2(0, 0), Color.Brown);

    base.Draw(gameTime);
}
\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .