Francisco Dias

Francisco Dias

  • NA
  • 3
  • 3.5k

Game State Machine good practices ?! Textbox focus problem.

Oct 30 2013 11:38 PM
I'm creating a Game using WPF (a text based game nothing too heavy).
I'm trying to handle the game states: TitleScreen, PlayScreen, PauseScreen... using a ScreenManager - a static class that adds a given screen to a list (stack) and places the most uptop screen in a MainWindow container (to be displayed).

The input is handled in a way I don't know if it is tottaly right:

TitleScreen inherits from Screen which inherits from UserControl - Screen is an abstract class that implements some methods and stores a MainWindow reference this way each Screen can register to the MainWindow KeyDown (event) and add it's input logic.

TitleScreen : [Key.A] goes to PlayScreen, [Key.Escape] exists
PlayScreen: [Key.B] goes to PauseScreen, [Key.Space] exists
...

My first question is: Is this the right way to go?! Everything till here works, but I wanted to know if there is a better way to handle input (this one seams a little odd). Or another way to handle the "state machine".

Now the text box problem... as I transit from TitleScreen to PlayScreen there is a textbox in PlayScreen used for inputing commands (I want it to be focus right from the beginning).

  1. As the G key is pressed during TitleScreen
  2. I instanciate the PlayScreen UserControl
  3. Remove the previous Screen (title)
  4. Place the new screen in the MainWindow container
  5. Set the focus to the text box.
  6. ERROR: Textbox has caret but caret is not blinking (Keyboard.Focus() doesn't seem to work either).
If I try to Focus TextBox with the help of the OnLoad event raised by the UserControl, the TextBox gets focus as expected but the "G" key used to trigger the transition is captured and appears written on the text box... and I don't seem to be able to clear it:

TextBox.Text = string.empty;
TextBox.Clear();

Don't work... I'm doing this for a while now I'm becoming desperate.
please help. Thank you