Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [SOLVED]Keyboard Input Lag  (Read 4620 times)
0 Members and 1 Guest are viewing this topic.
Offline duce

Senior Newbie





« Posted 2012-01-27 18:43:08 »

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
Offline ReBirth
« Reply #1 - Posted 2012-01-27 22:54:00 »

That's not stubborn, otherwise is Grin
try to let the JFrame to implement listener.

Offline duce

Senior Newbie





« Reply #2 - Posted 2012-01-28 01:56:39 »

Didn't help at all Sad It worked perfectly in my games made in Slick. Maybe something is wrong with my game loop? What is the purpose of SwingUtilities.invokeLater()?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie

JGO Kernel


Medals: 201



« Reply #3 - Posted 2012-01-28 02:22:47 »

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

Offline GabrielBailey74
« Reply #4 - Posted 2012-01-28 02:23:00 »

Believe it's something like:

1  
2  
3  
4  
5  
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.

Offline Spudgun

Junior Newbie





« Reply #5 - Posted 2012-01-28 03:27:54 »

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.
Offline Cero
« Reply #6 - Posted 2012-01-28 03:34:11 »

Didn't we just had Christmas =D

Offline duce

Senior Newbie





« Reply #7 - Posted 2012-01-28 04:29:00 »

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.
Offline ReBirth
« Reply #8 - Posted 2012-01-28 13:06:51 »

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.

Offline duce

Senior Newbie





« Reply #9 - Posted 2012-01-28 14:19:32 »

http://pastebin.com/mdEDW6Tx

Here's what fills my ArrayList
http://pastebin.com/AZLKQV63
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline duce

Senior Newbie





« Reply #10 - Posted 2012-01-28 14:24:57 »

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.
Offline ReBirth
« Reply #11 - Posted 2012-01-28 16:08:49 »

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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
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
}
}

Offline duce

Senior Newbie





« Reply #12 - Posted 2012-01-28 17:23:10 »

I tried what you said, but nothing changed.

http://pastebin.com/33reqdRy
Offline duce

Senior Newbie





« Reply #13 - Posted 2012-01-28 20:41:10 »

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

Senior Newbie





« Reply #14 - Posted 2012-01-28 20:48:11 »

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
Offline theagentd
« Reply #15 - Posted 2012-01-29 03:38:43 »

1  
g2d.scale(gameScale, gameScale);

Don't do this.

Myomyomyo.
Offline Shazer2

Junior Member


Medals: 3


Aspiring developer.


« Reply #16 - Posted 2012-01-29 03:45:23 »

Do this for the loop too, instead of doing blocks.get(i) everytime.
1  
2  
3  
for (Block block : blocks) {

}


That way you can access it by doing
1  
2  
3  
switch(block.get(j)) {

}


Just a bit of a cleaner way.

~Shazer2 Smiley

"When you want to be successful as bad as you want to breathe, then you will be successful." - Eric Thomas
Offline ReBirth
« Reply #17 - Posted 2012-01-29 04:35:14 »

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.

Offline theagentd
« Reply #18 - Posted 2012-01-29 04:57:35 »

Do this for the loop too, instead of doing blocks.get(i) everytime.
1  
2  
3  
for (Block block : blocks) {

}


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

Myomyomyo.
Offline Shazer2

Junior Member


Medals: 3


Aspiring developer.


« Reply #19 - Posted 2012-01-29 04:59:18 »

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

~Shazer2 Smiley

"When you want to be successful as bad as you want to breathe, then you will be successful." - Eric Thomas
Offline duce

Senior Newbie





« Reply #20 - Posted 2012-01-29 08:46:56 »

Thanks for the help, everyone! I managed to find the problem. I needed to use createCompatibleImage() to convert my images to the required format.

Here is my new Texture class:
http://pastebin.com/9S8QMXby
Offline sproingie

JGO Kernel


Medals: 201



« Reply #21 - Posted 2012-01-30 18:47:32 »

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

This is like telling someone driving a fully loaded 18-wheeler to take the bumper stickers off to shed weight.  The point is to get familiar with programming idioms that are actually clear.
Offline theagentd
« Reply #22 - Posted 2012-01-30 20:08:36 »

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

This is like telling someone driving a fully loaded 18-wheeler to take the bumper stickers off to shed weight.  The point is to get familiar with programming idioms that are actually clear.

That depends on how much you use it. Tell someone that it's okay to do use those for-loops and he/she is going to use them for nested for-loops. I've saved milliseconds by changing it to a normal for-loop, and I don't have many of those you know.

Myomyomyo.
Offline pitbuller
« Reply #23 - Posted 2012-01-30 21:32:34 »

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

This is like telling someone driving a fully loaded 18-wheeler to take the bumper stickers off to shed weight.  The point is to get familiar with programming idioms that are actually clear.

That depends on how much you use it. Tell someone that it's okay to do use those for-loops and he/she is going to use them for nested for-loops. I've saved milliseconds by changing it to a normal for-loop, and I don't have many of those you know.
Just for additional notes that on android for:each loop is faster that indexed loop and do not produce iterator but only for normal array. For collections that cannot be indexed in constant time they are faster and create garbage but  ArrayList they are slower. I used lot of time with timers and byte/dex code to get this conclusion.

Ps. Don't say its does not matter much. I know it allready.
Offline sproingie

JGO Kernel


Medals: 201



« Reply #24 - Posted 2012-01-30 21:45:17 »

"Optimizing" the iterator out of a brute-force algorithm.  Bikeshedding at its finest.
Offline theagentd
« Reply #25 - Posted 2012-01-31 06:02:02 »

"Optimizing" the iterator out of a brute-force algorithm.  Bikeshedding at its finest.

-_-

Let's just say I'm not using iterators in my particle engine that runs 1 million particles, or in the ray-casting method I have to check visibility between two points on a tile-map.

Myomyomyo.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (24 views)
2014-07-24 01:59:36

Riven (24 views)
2014-07-23 21:16:32

Riven (18 views)
2014-07-23 21:07:15

Riven (21 views)
2014-07-23 20:56:16

ctomni231 (50 views)
2014-07-18 06:55:21

Zero Volt (45 views)
2014-07-17 23:47:54

danieldean (36 views)
2014-07-17 23:41:23

MustardPeter (39 views)
2014-07-16 23:30:00

Cero (54 views)
2014-07-16 00:42:17

Riven (55 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!