Right now, I'm able to read the score to a text file every time the game is won. No problem there. Now I'm just having a bit of trouble asking the player to input a name.
I'm going to have a screen pop up asking the player to enter his/her name. As the player presses a key, say a 'z' for example I want that drawn to the screen.
After researching a bit, I came up with what I figured would be an appropriate algorithm, but I'm having a little bit of trouble. Here's the code:
public override void Draw(GameTime gameTime)
{
spriteBatch.Draw(image, imageRectangle, Color.White);
KeyboardState keyboardState = Keyboard.GetState();
string playerName = "";
Keys[] pressedKeys = keyboardState.GetPressedKeys();
int i = 0;
while (!keyboardState.IsKeyDown(Keys.Enter))
{
playerName += pressedKeys[i].ToString();
i++;
spriteBatch.DrawString(spriteFont, playerName, new Vector2(Game.window.ClientBounds.Width / 2,
Game.window.ClientBounds.Height / 2), Color.White);
}
base.Draw(gameTime);
}
The run time error is occurring here:
playerName += pressedKeys[i].ToString();
At the point where this screen is suppose to pop up, the game freezes. I get an error that says: IndexOutOfRangeException was unhandled. Index was outside the bounds of the array.
I suppose I can see why this is happening, but not sure how to fix it. I'm guessing the problem is in my logic. I'm not exactly sure how to get the input from the user and print it out one at a time.
Help would be greatly appreciated!
Thanks!
This post has been edited by eZACKe: 04 July 2010 - 11:08 AM

New Topic/Question
Reply



MultiQuote




|