Trying to start a game when 'Enter' is pressed.
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..
Code :
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...
Code :
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:
Code :
public void keyPressed(KeyEvent e) {
...
else if (e.getKeyCode() == KeyEvent.VK_ENTER){
enterPressed = true;
}
}
Thanks.
Re: Trying to start a game when 'Enter' is pressed.
The Enter key may be different because of its use for Action events.
Look at using Key Binding.
Re: Trying to start a game when 'Enter' is pressed.
Some things to look at:
1. Did you indeed add the KeyListener to that component?
2. Does the component have the focus (and thus is able to receive any KeyEvents)?
Re: Trying to start a game when 'Enter' is pressed.
I don't know what Key Bindings are, however I think I may just change it to pressing a JButton to start the game instead... Hopefully this will work. :| I've definitely added the listener to the component.
Re: Trying to start a game when 'Enter' is pressed.
Quote:
I don't know what Key Bindings are,
Go to this site and Find Key Bindings:
The Really Big Index
Re: Trying to start a game when 'Enter' is pressed.
I have been advised to not use key bindings by my tutor.. Is there a way to do this without it? Surely the code above should work!! Even when I change it to another key being pressed for example 's' it should always be 'listening' for key presses... And therefore should carry out the action performed method! But it never even accesses that method. Is it legit to call a method of the class when an action is performed? For example i am a calling this.runGame() when enter is pressed.
Re: Trying to start a game when 'Enter' is pressed.
Are you able to get any keypress to call your keylistener method? Does the component with the listener have focus?
Quote:
Is it legit to call a method of the class when an action is performed?
Yes, you should be able to call a method. What is the "action" that is performed? A key press.