Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Trying to start a game when 'Enter' is pressed.

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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..
    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.
    Last edited by Shaybay92; September 24th, 2011 at 05:41 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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)?

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Trying to start a game when 'Enter' is pressed.

    I don't know what Key Bindings are,
    Go to this site and Find Key Bindings:
    The Really Big Index

  6. #6
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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?
    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.

Similar Threads

  1. Is Key Pressed statements
    By Yo Cas Cas in forum AWT / Java Swing
    Replies: 6
    Last Post: August 27th, 2011, 12:48 AM
  2. i dont know why i cant see the output nor enter input
    By Newbiesss in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 1st, 2011, 12:41 PM
  3. Read in file and store in 2D array start of The Game of Life
    By shipwills in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 2nd, 2011, 09:52 AM
  4. Move button with mouse pressed
    By Stavros in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 10th, 2010, 08:45 AM
  5. how to know user pressed a key in the keyboard
    By cilang in forum File I/O & Other I/O Streams
    Replies: 16
    Last Post: September 11th, 2009, 10:08 AM