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 am having a problem drawing the "Game Over!" text once the user is on the last tile.

This is what I have:

The Update and drawText methods are in a class named turtle:

public void Update(float scalingFactor, int[,] map, SpriteBatch batch, SpriteFont font)
{
    if (isMovable(mapX, mapY - 1, map))
    {
        position.Y = position.Y - (int)scalingFactor;
        angle = 0.0f;
        Program.form.direction = "";

        if (mapX == 17 && mapY == 1)// This is the last tile(Tested)
        {
            Program.form.BackColor = System.Drawing.Color.Red;
            drawText(batch, font);
        }
    }
}                    

public void drawText(SpriteBatch spritebatch, SpriteFont spriteFont)
{
    textPosition.X = 200; // a vector2
    textPosition.Y = 200;
    spritebatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
    spritebatch.DrawString(spriteFont, "Game Over!!!", textPosition, Color.Red);
    spritebatch.End();
}

This update is in the Game1 class:

protected override void Update(GameTime gameTime)
{
    // Allows the game to exit
    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
        this.Exit();

    turtle.Update(scalingFactor, map, spriteBatch, font);         

    base.Update(gameTime);
}

I have also added the font content to LoadContent:

font = Content.Load<SpriteFont>("fontType");

What am I doing wrong? Why does the text not want to show on game completion? If I call the turtle.draw() in the main Draw method. The "Game Over" text stays on screen from the beggining. What am I missing?

Thanks

share|improve this question
    
Without knowing more, have you tested this line? if (mapX == 17 && mapY == 1) –  Woody Zantzinger Jun 1 '12 at 18:39
    
Yes ive tested it, that is the last tile. The way i tested it was when the sprite moved onto that tile, my WinForm will change color. –  Sigh-AniDe Jun 1 '12 at 18:41
    
Your font is being initialized correctly? –  Woody Zantzinger Jun 1 '12 at 18:45
    
Yes, it is, Byte56 answer is correct –  Sigh-AniDe Jun 1 '12 at 18:50
    
The question was pretty much answered ,but i just wanted to say that probably the text does get drawn the thing is the update loop continues and you receive input and the if statement becomes false and you don't notice the text being drawn. –  Artii Jun 3 '12 at 12:12

1 Answer 1

up vote 5 down vote accepted

You want to keep your drawing and update loops separate. Attempting to draw inside the update loop is likely to cause issues. Additionally, putting logic for drawing a game over message inside the update loop of a game character isn't the best place for it.

You can try using a global variable for setting when your game is over:

if (mapX == 17 && mapY == 1)
{
    GameOver = true;
}

Then in your draw loop,

if (GameOver)
{
    Program.form.BackColor = System.Drawing.Color.Red;
    drawText(batch, font);
}

This will allow you to have a bit more control in other aspects as well. For example you can check to see if the game is over before checking for input. That would disallow players from moving around after the game ended.

share|improve this answer
    
Dude you are a gaming guru! Thanks a million. –  Sigh-AniDe Jun 1 '12 at 18:49

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.