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/0LTdKr21EDIT: Solved. Needed to convert my images during initialization. http://pastebin.com/9S8QMXby
|
|
|
|
|
ReBirth
|
 |
«
Reply #1 - Posted
2012-01-27 22:54:00 » |
|
That's not stubborn, otherwise is  try to let the JFrame to implement listener.
|
|
|
|
duce
Senior Newbie 
|
 |
«
Reply #2 - Posted
2012-01-28 01:56:39 » |
|
Didn't help at all  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!
|
|
sproingie
|
 |
«
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! 
|
|
|
|
|
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() { }; } |
You'd want it in your main loop of course, you wouldn't want to call it upon just start of the program.
|
|
|
|
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.
|
|
|
|
|
Cero
|
 |
«
Reply #6 - Posted
2012-01-28 03:34:11 » |
|
Didn't we just had Christmas =D
|
|
|
|
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.
|
|
|
|
|
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.
|
|
|
|
duce
Senior Newbie 
|
 |
«
Reply #9 - Posted
2012-01-28 14:19:32 » |
|
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
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.
|
|
|
|
|
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(){ } void gameLoop(){ while(gameRunning){ } } public static void main(String[] args) { Yours y = new Yours(); y.gameLoop(); } ... class X implements KeyListener{ } } |
|
|
|
|
|
|
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.
|
|
|
|
|
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
|
|
|
|
|
theagentd
|
 |
«
Reply #15 - Posted
2012-01-29 03:38:43 » |
|
1
| g2d.scale(gameScale, gameScale); |
Don't do this.
|
Myomyomyo.
|
|
|
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 
|
"When you want to be successful as bad as you want to breathe, then you will be successful." - Eric Thomas
|
|
|
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.
|
|
|
|
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  That's generally slower since this creates an Iterator instance every frame. Since when did accessing array elements become slow?
|
Myomyomyo.
|
|
|
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 
|
"When you want to be successful as bad as you want to breathe, then you will be successful." - Eric Thomas
|
|
|
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
|
|
|
|
|
sproingie
|
 |
«
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.
|
|
|
|
|
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.
|
|
|
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.
|
|
|
|
|
sproingie
|
 |
«
Reply #24 - Posted
2012-01-30 21:45:17 » |
|
"Optimizing" the iterator out of a brute-force algorithm. Bikeshedding at its finest.
|
|
|
|
|
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.
|
|
|
|