Results 1 to 3 of 3
- 07-29-2009, 07:20 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 2
- Rep Power
- 0
[Solved] 2 Key Events at once
I am making a 2 player game that needs two objects to move around.
In a KeyEvent, it detects when I move Player 1 Up and Down (jLabel7) using the Up key and the Down key.
In the same KeyEvent, it detects when I can move Player 2 Up and Down (jLabel6) using the W key (up) and the S key (down).
Both ships work fine. When I press Up, it activates a timer and moves jLabel7 up.
When I press Down, it activates a timer and moves jLabel7 down.
When I press W, it activates a timer and moves jLabel6 up.
When I press S, it activates a timer and moves jLabel6 down.
Also, I have a KeyRelease event that detects when these keys were released, and it sets a boolean to false, letting the timers know they can shut off.
So all is well right? wrong. If I try to hold Up and S together for instance, moving each jLabel in a direction, it will activate their timers, moving them. If I release the keys, it will not detect a Keyrelease on one of the jLabels and the jLabel won't stop, even if i keep hitting all four keys to try and reregister a KeyRelease. The ship that stopped, now wont move unless i hold one of the keys that moved the ship that is stuck moving, then press it's keys. For example:
I press Up (jLabel7 moves up) and S (jLabel6 moves down) at the same time. I let go of both keys, jLabel7 stops moving, jLabel6 is stuck going down no matter what i do. Now jLabel7 will not move unless I hold a key that was supposed to move jLabel6 THEN press a jLabel7 key.
Now I'm guessing, that it is because pressing multiple keys at once won't work because they aren't in separate threads, they get all funny because the KeyPressed Action Event is only working once and can't work with all these seperate key presses and it gets stuck somewhere. Each press of a key activates a Timer/thread, but it itself isn't one.
What is the error and the best way to fix it?
Java Code:private void jButton1KeyPressed(java.awt.event.KeyEvent evt) { //KEY UP TIMER ActionListener timerUp = new ActionListener() { public void actionPerformed(ActionEvent evt) { jLabel7.setLocation(jLabel7.getX(),jLabel7.getY()-2); repaint(); //STOPS TIMER if(Ship1Up == false){ timer.stop(); } } }; //KEY UP if(evt.getKeyCode() == KeyEvent.VK_UP && Ship1Up == false){ Ship1Up = true; Ship1Down = false; //PLAYS UP TIMER if(Ship1Up == true){ timer = new Timer(20, timerUp); timer.start(); } } //KEY DOWN TIMER ActionListener timerDown = new ActionListener() { public void actionPerformed(ActionEvent evt) { jLabel7.setLocation(jLabel7.getX(),jLabel7.getY()+2); repaint(); //STOPS TIMER if(Ship1Down == false){ timer.stop(); } } }; //KEY DOWN if(evt.getKeyCode() == KeyEvent.VK_DOWN && Ship1Down == false){ Ship1Down = true; Ship1Up = false; //PLAYS DOWN TIMER timer = new Timer(20, timerDown); timer.start(); } //KEY W TIMER ActionListener timerW = new ActionListener() { public void actionPerformed(ActionEvent evt) { jLabel6.setLocation(jLabel6.getX(),jLabel6.getY()-2); repaint(); //STOPS TIMER if(Ship2Up == false){ timer.stop(); } } }; //KEY W if(evt.getKeyCode() == KeyEvent.VK_W && Ship2Up == false){ Ship2Up = true; //PLAYS W TIMER timer = new Timer(20, timerW); timer.start(); } //KEY S TIMER ActionListener timerS = new ActionListener() { public void actionPerformed(ActionEvent evt) { jLabel6.setLocation(jLabel6.getX(),jLabel6.getY()+2); repaint(); //STOPS TIMER if(Ship2Down == false){ timer.stop(); } } }; //KEY S if(evt.getKeyCode() == KeyEvent.VK_S && Ship2Down == false){ Ship2Down = true; //PLAYS DOWN TIMER timer = new Timer(20, timerS); timer.start(); } } private void jButton1KeyReleased(java.awt.event.KeyEvent evt) { //KEY UP RELEASED if(evt.getKeyCode() == KeyEvent.VK_UP){ Ship1Up = false; } //KEY DOWN RELEASED if(evt.getKeyCode() == KeyEvent.VK_DOWN){ Ship1Down = false; } //KEY UP RELEASED if(evt.getKeyCode() == KeyEvent.VK_W){ Ship2Up = false; } //KEY DOWN RELEASED if(evt.getKeyCode() == KeyEvent.VK_S){ Ship2Down = false; } }Last edited by Gheta; 07-29-2009 at 08:52 PM.
- 07-29-2009, 08:50 PM #2
Member
- Join Date
- Jul 2009
- Posts
- 2
- Rep Power
- 0
Sorry for the double post but I figured out multiple key presses for anyone else having this problem.
1 You need to have booleans to remember the key press state (press a key, a boolean becomes true, release the key and the boolean becomes false, also have a timer.)
2 This was my error. At the beginning of all the code, I declared:
Timer timer;
when I should have declared separately for each key:
Timer upTimer;
Timer downTimer;
Timer wTimer;
Timer sTimer;
This solved my issue and multiple keys can work at once for multiple objects on screen.
-
I recommend using a Timer and using Key Binding for this problem. For an example look at my code in post 6 of this thread: How to simply get if a key is pushed?
Similar Threads
-
Swing Timers Issue.
By killpoppop in forum AWT / SwingReplies: 4Last Post: 03-09-2009, 11:17 PM -
Question on swing timers
By Samgetsmoney in forum New To JavaReplies: 5Last Post: 02-20-2009, 07:34 AM -
Concurrent timers are not getting invoked for same timer info (Serializable object co
By Neeraj in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 12-31-2008, 02:20 PM -
Swing Ide?
By makpandian in forum AWT / SwingReplies: 2Last Post: 12-28-2008, 05:07 PM -
map javax.swing.text.Element to javax.swing.text.View
By elizabeth in forum New To JavaReplies: 1Last Post: 07-30-2007, 07:02 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks