Skip to main content
gamedev.net
🔒 Locked

[java] Main game loop problem

Started by LinkOfTime 5 replies 1.3k views
Original Post
LinkOfTime
LinkOfTime
Hi all. The problem I have is that ever since I added a main game loop to my game it seems to slow the whole game down (button press events and other things like that...). Now, my question is if there is a command I should add to the loop, like "doEvents()" in VB, to speed the game up? Thanks in advance for the help :)
Don''t have one, sorry.
DantarionX
DantarionX
Your game is probably looping so fast that its using 100% cpu power, making keyboard events seem sluggish.

You need to lock the game to a framerate, or make it wait and allow the cpu to do other things. This topic applies to all programming languages, so it shouldn't be hard to find information on it.
~Dantar
shmoove
shmoove
Thread.sleep().

shmoove
DantarionX
DantarionX
Yep, that should do it.
~Dantar
LinkOfTime
LinkOfTime
Ok, thanks for the help :)
I used Thread.sleep(1000) and the game is running faster but, the "Exit" command still doesn't work... I mean, I press the "Exit" button in my game and the game doesn't quit.
Now, if I disable the main game loop then the "Exit" command works.
One thing I want to emphasize is that, for testings, I only left the Thread.sleep() command in the loop and the "Exit" command still won't work when the loop exists...

Thanks again for all the help :)
Don''t have one, sorry.
mako_5
mako_5
Quote:
Original post by LinkOfTime
Ok, thanks for the help :)
I used Thread.sleep(1000) and the game is running faster but, the "Exit" command still doesn't work... I mean, I press the "Exit" button in my game and the game doesn't quit.
Now, if I disable the main game loop then the "Exit" command works.
One thing I want to emphasize is that, for testings, I only left the Thread.sleep() command in the loop and the "Exit" command still won't work when the loop exists...

Thanks again for all the help :)


Just a note: Thread.sleep(1000) will wait for one entire second (the parameter is in milliseconds). Unless this is what you want, I'd go with a smaller value such as 16 approximates at just under 60 frames a second for better performance.
daviangel
daviangel
Quote:
Original post by LinkOfTime
Ok, thanks for the help :)
I used Thread.sleep(1000) and the game is running faster but, the "Exit" command still doesn't work... I mean, I press the "Exit" button in my game and the game doesn't quit.
Now, if I disable the main game loop then the "Exit" command works.
One thing I want to emphasize is that, for testings, I only left the Thread.sleep() command in the loop and the "Exit" command still won't work when the loop exists...

Thanks again for all the help :)

If the code for your exit button isn't in your main loop you will need to add some code to gameloop to check for keypress.

[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.