0

I am developing a small game applet and I would like for it to wait until the user presses spacebar to start, however I am not sure what the best approach for this would be. Here I have written a SSCCE of a moving box.

http://pastebin.com/Zq1eMEJ9

Any suggestions would be greatly appreciated!

phcoding
  • 608
  • 4
  • 16
  • 1
    Bind the start() method to your key action listener for the spacebar? OR rather, create an action listener for your spacebar and call the start() method within it when the actionPerformed() gets called. – Nico Feb 16 '13 at 22:03
  • 2
    You can paste your code directly in the question without referring to pastebin. – iTech Feb 16 '13 at 22:07

1 Answers1

3

Hope this sorts your problem...

public void keyTyped(KeyEvent evt) {
    if (evt.getKeyChar() == ' ') {
           Thread th = new Thread(movingBoxInstance);
           th.start();
    }
}
hd1
  • 33,938
  • 5
  • 80
  • 91
  • 1
    IMHO keyListener is a bad choice as it suffers from issues with focus (unless the component has direct focus, the keyListener won't be fired). If using Swing, key bindings are a better (and generally safer) choice. If using AWT, the keyListener is the only practical choice – MadProgrammer Feb 16 '13 at 23:34
  • Thanks MadProgrammer that is really useful information, the solution provided did seem a little more like a hack than a solution. – phcoding Feb 16 '13 at 23:42