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.
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.

share|improve this question
    
Do you call GraphicsDevice.Clear() anywhere? –  The Communist Duck Jul 8 '11 at 10:11
    
I think you need to learn how loops work. –  AttackingHobo Jul 8 '11 at 16:51
    
@AttackingHobo I would say OP needs to learn about variable scope first... –  Nate Jul 8 '11 at 21:51
add comment

1 Answer 1

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);
}
share|improve this answer
add comment

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.