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?
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; } }