Results 1 to 7 of 7
Thread: Simple problem
- 12-03-2011, 04:15 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 12
- Rep Power
- 0
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?
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.Java Code:if(keyin.equals(//spacebar)) { bullet.isAlive==true; }
-
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?
- 12-03-2011, 04:41 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 12
- Rep Power
- 0
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()
-
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?
- 12-03-2011, 05:04 AM #5
Member
- Join Date
- Nov 2011
- Posts
- 12
- Rep Power
- 0
Re: Simple problem
I added the key listener to my init method i.e.
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 spacebarJava Code:public void init() { addKeyListener(this); }
-
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.
- 12-03-2011, 05:26 AM #7
Member
- Join Date
- Nov 2011
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
Simple program, simple problem
By taymilll in forum New To JavaReplies: 12Last Post: 06-20-2011, 05:12 AM -
Simple problem
By melovehockey in forum New To JavaReplies: 8Last Post: 12-29-2010, 01:43 AM -
Need help with simple problem
By newb_tewb in forum New To JavaReplies: 3Last Post: 04-08-2010, 02:18 AM -
simple line problem / for loop problem
By helpisontheway in forum New To JavaReplies: 1Last Post: 11-17-2009, 06:12 AM -
simple GUI problem
By McChill in forum New To JavaReplies: 4Last Post: 04-03-2009, 03:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks