Sign in to follow this  

[java] Main game loop problem

This topic is 4029 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4029 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this