Results 1 to 7 of 7

Thread: Simple problem

  1. #1
    beijct is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Simple problem

    Hi all, I've just got a simple question; how do I make the computer understand and react to my pressing space bar in the keyTyped method?

    Java Code:
    if(keyin.equals(//spacebar))
    			{
    				bullet.isAlive==true;
    			}
    I just need the //spacebar filled in with the proper code, I looked around online but I didn't really understand it. Thanks in advance.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Simple problem

    This is not simple, not in the least bit. What is a "keyTyped" method? What type of program is this? Swing? AWT? SWT? Console?

  3. #3
    beijct is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Re: Simple problem

    The class imports java.awt.event.* and implements KeyListener
    here is the whole method:
    Java Code:
    public void keyTyped( KeyEvent event )
    	{
    		String keyin;
    		keyin = ""+event.getKeyText( event.getKeyCode());
    		
    		if(keyin.equals(//spacebar))
    			{
    				bullet.isAlive==true;
    			}
    	}//keyTyped()

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Simple problem

    For one bullet.isAlive == true isn't an assignment statement but rather a boolean statement to check if isAlive is true or not. I think that you want bullet.isAlive = true; or better bullet.setAlive(true);. For another do you add the KeyListener to a component? Does this component have the focus? Have you read the tutorial on using KeyListeners?

  5. #5
    beijct is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Re: Simple problem

    I added the key listener to my init method i.e.
    Java Code:
    public void init()
    	{
    		addKeyListener(this);
    	}
    I also have a keyPressed method and a keyReleased method as per my teacher's instructions and they both work fine, I just don't know what I'd put in those perentheses to make the computer understand I'm referring to the spacebar

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Simple problem

    I think that you may want to create a small application that just tests use of a KeyListener and listening for the space button. Then if it doesn't work, you can post this small compilable and runnable program here and we can analyze it and help you correct it. As for me, I usually use Key Bindings for this sort of thing, not a KeyListener, and I usually deal with KeyEvent.VK_ codes such as KeyEvent.VK_SPACE.

  7. #7
    beijct is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Re: Simple problem

    Alright, I'll give that a shot, thanks so much for your help and patience!

Similar Threads

  1. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 06:12 AM
  2. Simple problem
    By melovehockey in forum New To Java
    Replies: 8
    Last Post: 12-29-2010, 02:43 AM
  3. Need help with simple problem
    By newb_tewb in forum New To Java
    Replies: 3
    Last Post: 04-08-2010, 03:18 AM
  4. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 07:12 AM
  5. simple GUI problem
    By McChill in forum New To Java
    Replies: 4
    Last Post: 04-03-2009, 04:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •