[SOLVED]Keyboard Input Lag

I’m trying to make a game in Java. I decided to be stubborn, and not use LWJGL or Slick. Everything went fine until input. The keyboard arrow presses are delayed, and I don’t know what is causing it.

http://pastebin.com/0LTdKr21

EDIT:
Solved. Needed to convert my images during initialization.
http://pastebin.com/9S8QMXby

That’s not stubborn, otherwise is ;D
try to let the JFrame to implement listener.

Didn’t help at all :frowning: It worked perfectly in my games made in Slick. Maybe something is wrong with my game loop? What is the purpose of SwingUtilities.invokeLater()?

“Many thousands are in want of common answers; hundreds of thousands are in want of common knowledge, sir.”

“Are there no search engines?”

"Plenty of search engines, said the gentleman, laying down his keyboard again.

“And the javadoc on Oracle’s website”, demanded Sproingie, “is it still in operation?”

“Both very busy sir.”

“Oh. I was afraid, from what you said at first, that something had occurred to stop them in their useful course,” said Sproingie. “I’m very glad to hear it.”

Humbug! :cranky:

Believe it’s something like:


public void init() {
SwingUtilities.invokeLater() {
//Than add the action listener for the event performed here?
};
}

You’d want it in your main loop of course, you wouldn’t want to call it upon just start of the program.

I came across ‘delays’ in my early game efforts which seemed to be related to how key repeats are handled. The way to demonstrate the effect is to open a text editor and hold down a letter key. After the first letter is displayed there is a noticeable delay after which the letter then repeats quickly (in Windows at least). I would get the same responses in my game.

My way round it is rather than respond to action directly in the overridden keylistener methods, I use them to set booleans which eliminate the ‘lag’ as the game movement logic now relies on the boolean values rather than the raw keypress data.

I’m not sure if this is the same problem you are experiencing, but from what you are describing it sounds very familiar to my experiences.

Didn’t we just had Christmas =D

I tried having the key listeners set flags, but it still delays. I had my listeners do println()'s, and the message doesn’t delay, but the movement does.

Then something happend on your framerate, which can be caused by busy logic or drawing lack. It’s pretty scary you did duo run method there, plus I saw nested ArrayList called blocks. I suggest you to narrow down the main class. Also, you use singleton Textures, so post it may help.

http://pastebin.com/mdEDW6Tx

Here’s what fills my ArrayList
http://pastebin.com/AZLKQV63

Okay, so to test the FPS of my game, I loaded Fraps up to see the counter. The game counts up from 0 to eight frames per second, then stops there. I have no idea why.

I see. Your code is too mess. Even on singleton you call another singleton just to load image. Nothing wrong on LevelLoader class. So you can take this first aid: change the structure. avoid nested loop. you actually no need to use thread, since your gameloop method acts similiar. For example:


public class Yours extends Canvas{
public Yours(){
//init the Canvas
//add listener (below class)
//call JFrame, init and add(this)
}
void gameLoop(){
while(gameRunning){
//update, render, sleep, FPS calculation
}
}
public static void main(String[] args) {
Yours y = new Yours();
y.gameLoop();
}
...
class X implements KeyListener{
//do it
}
}

I tried what you said, but nothing changed.

http://pastebin.com/33reqdRy

The FPS is capped at 8. If I set it to update slower, it will. But higher than 8 will just result in 8.

When turning off the drawing of the level, it significantly improves the FPS, and the listener responds quickly. Here is the Level class and its draw function.

http://pastebin.com/TmuFW3te

g2d.scale(gameScale, gameScale);

Don’t do this.

Do this for the loop too, instead of doing blocks.get(i) everytime.


for (Block block : blocks) {

}

That way you can access it by doing


switch(block.get(j)) {

}

Just a bit of a cleaner way.

~Shazer2 :slight_smile:

Like theagentd said. Also, if your blocks has limited size (not growing on fly) or you know the max size that never will be exceeded, I suggest to use 2D array.

That’s generally slower since this creates an Iterator instance every frame. Since when did accessing array elements become slow?

It is what I was told in #lwjgl by sproingie.

~Shazer2 :slight_smile: