(This has been posted in another forum, but it often takes time to get help there and I really need to solve this. I hope you understand and I'll appreciate your help).
Sorry for not finding the right sub-forum for this.
I got Key Bindings for a game I'm working on. They're supposed to move objects when keys are pressed.
(currently, each println(..) represents actual code to move things on the screen, will be added later).
The printing in the console works, but with delays. The words "left", "right", etc. appear with a delay in the console, sometimes half a second delay, sometimes a few seconds delay.
There is no delay at all sometimes, but when a lot of keys are pressed one after the other, there's a delay of several seconds, and only then the words suddenly appear in the console (like the computer had too much to handle at the same time).
When I did the same Key Bindings in a different project without all the game logic, only pressing buttons that print words in the console - it worked perfectly without delay.
So I suspect the problem is something with the game code, or the way I used Key Bindings inside the game code.
How do I fix this?
I tried putting the Key Bindings code in the thread that contains the game-loop (before the game-loop), and in the constructor. Same problem.
Here is the relevant code of the Board class (extending JPanel), the main JPanel of the game which displays the graphics and manipulates objects.
import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Board extends JPanel implements Runnable { Tank tank1,tank2; boolean[] keysPressed1,keysPressed2; Action upAction,leftAction,rightAction,wAction,aAction,dAction; InputMap inputMap; ActionMap actionMap; public Board(){ setFocusable(true); setBackground(Color.BLACK); tank1 = new Tank("red"); tank2 = new Tank("blue"); inputMap = this.getInputMap(); actionMap = this.getActionMap(); Thread gameloop = new Thread(this); gameloop.start(); } public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; // Drawing tanks on the screen. } public void run(){ // Key Bindings ////// upAction = new AbstractAction(){ public void actionPerformed(ActionEvent e){ System.out.println("up"); } }; leftAction = new AbstractAction(){ public void actionPerformed(ActionEvent e){ System.out.println("left"); } }; rightAction = new AbstractAction(){ public void actionPerformed(ActionEvent e){ System.out.println("right"); } }; wAction = new AbstractAction(){ public void actionPerformed(ActionEvent e){ System.out.println("w"); } }; aAction = new AbstractAction(){ public void actionPerformed(ActionEvent e){ System.out.println("a"); } }; dAction = new AbstractAction(){ public void actionPerformed(ActionEvent e){ System.out.println("d"); } }; inputMap.put(KeyStroke.getKeyStroke("UP"),"upAction"); inputMap.put(KeyStroke.getKeyStroke("LEFT"),"leftAction"); inputMap.put(KeyStroke.getKeyStroke("RIGHT"),"rightAction"); inputMap.put(KeyStroke.getKeyStroke("W"),"wAction"); inputMap.put(KeyStroke.getKeyStroke("A"),"aAction"); inputMap.put(KeyStroke.getKeyStroke("D"),"dAction"); actionMap.put("upAction",upAction); actionMap.put("leftAction",leftAction); actionMap.put("rightAction",rightAction); actionMap.put("wAction",wAction); actionMap.put("aAction",aAction); actionMap.put("dAction",dAction); // End Key Bindings ////// // Start of game loop. //// int TICKS_PER_SECOND = 50; int SKIP_TICKS = 1000 / TICKS_PER_SECOND; int MAX_FRAMESKIP = 10; long next_game_tick = System.currentTimeMillis(); int loops; boolean game_is_running = true; while( game_is_running ) { loops = 0; while( System.currentTimeMillis() > next_game_tick && loops < MAX_FRAMESKIP) { // Code to manipulate tank1. // ....... tank1.move(); // Code to manipulate tank2. // ....... tank2.move(); next_game_tick += SKIP_TICKS; loops++; } repaint(); } } }
As I said, I suspect the program demands too much of the computer, so pressing a lot of keys one after the other, freezes the program for several seconds, and then suddenly prints the words on the console.
Thank you for the help