I have a game working successfully with arrow keys, so I know my KeyListener class is fine (canvas is an object of the KeyListener class). So I wanted to add a menu to my game, so that it will have a title and wait for the user to press enter to begin the game. So here we have..
public static void main(String[] args) { MazeTeeter sim; System.out.println("Starting Game."); sim = new MazeTeeter(); sim.menu(); } public void menu(){ Graphics g = canvas.getOffscreenGraphics(); g.setColor(Color.BLACK); g.fillRect(0, 0, xcanvas, ycanvas); g.setColor(Color.WHITE); g.setFont(new Font("SANS_SERIF", Font.ITALIC,100)); g.drawString("MazeTeeter", xcanvas/2 - 300, 200); canvas.drawOffscreen(); }
Now at this point I want it to stay on the menu screen until enter is pressed at which time canvas.enterPressed will become true and the game will start running...
public void actionPerformed(ActionEvent event) { int startGame = 0; if (startGame == 0 && canvas.enterPressed){ startGame ++; this.runGame(); } ...... }
For some reason it just wont pick up the event of pressing the Enter key, and so it sticks on the menu screen. Any ideas?
Btw the code for the enter press is here:
public void keyPressed(KeyEvent e) { ... else if (e.getKeyCode() == KeyEvent.VK_ENTER){ enterPressed = true; } }
Thanks.