Drawing text from update method in XNA
Posted
by
Sigh-AniDe
on Game Development
See other posts from Game Development
or by Sigh-AniDe
Published on 2012-06-01T18:30:41Z
Indexed on
2012/06/01
22:51 UTC
Read the original article
Hit count: 322
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
© Game Development or respective owner