Pergunta

I'm currently working on making an XNA 2D platformer, and I'm wondering if it's possible to draw a kind of static GUI over the game?

What i mean here is that while the game updates and the players position changes etc, the GUI would be drawn on a kind of static layer (overlay?), that would be the size of the window the game is being run in. That way the game wouldn't have to constantly update the GUI's position and thus be a little bit nicer to handle.

Any ideas?

Thanks for your time

Foi útil?

Solução

Yeah, you can just draw them using spritebatch :)

//HudTexture is a transparent texture the size of the window/screen, with hud drawn onto it.
SpriteBatch.Draw(HudTexture, Vector2.Zero, Color.White);
//Let's say that it works well to draw your score at [100,100].
SpriteBatch.DrawString(HudFont, Points.ToString(), new Vector2(100,100), Color.White);

if you plan on making more GUI (buttons etc) you might want to check out this answer I wrote to an other post: XNA DrawString() draws only a partial string

Also; For drawing scores etc, I have experienced quite a bit of boxing/unboxing because of ToString. For that reason, I have often had an array of strings to represent the score:

String[] ScoreText = new String[100000]; //As big as your max score

//In Initializing method:
for (int i = 0; i < ScoreText.Length; i++)
    ScoreText[i] = i.ToString();

//In draw:
SpriteBatch.DrawString(HudFont, ScoreText[Points], new Vector2(100,100), Color.White);

Outras dicas

Sure, you can draw any sprites wherever you want using a SpriteBatch for example.

How you manage it is up to you. Just don't give any transformation matrices and things will be drawn at the coordinates you specify.

You'd end up with drawing your level (which would be offset by some camera position for example), then draw your GUI elements on top of that, with no offsets.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top