Archived

This topic is now archived and is closed to further replies.

[java] Game doesn't run very fast... bad programminG?

This topic is 5611 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 I''m programming a RPG battle system and I''m having some problems with the animations. To avoid using thread.sleep (since that involved repeating the same code many times) I did the following code (example): aniatt[0] = false; aniatt[1] = false; time = 500; timeset = 500; if ((aniatt[0] == false) && (aniatt[1] == false)) { //load image 1 if (time > 0) { time--; } else { aniatt[0] = true; time = timeset; } } if ((aniatt[0] == true) && (aniatt[1] == false)) { //load image 1 if (time > 0) { time--; } else { aniatt[1] = true; time = timeset; } } My problem is, it takes around 1 min and 30 seconds to time var (which starts at 500) to get to 0 which forces me to give it a value o 20. What do you think? I''m either programming something wrong, it''s slow because I''m programming the game on a p133 or it''s just normal since I have lots of code before this animation loads? My problem is, if I had a thread sleep in the update() to make sure the game runs just fine in every pc, with the time == 20 it''ll either be just to slow or just to fast:| Telmo Amaro -- www.codingdreams.xrs.net

Share this post


Link to post
Share on other sites
You need to learn to finess java. Using a loop as a timer is just bad programming to start, and is inherently against how java works. You have to remeber that you cannot guarantee that your function will not be JIT compiled into native code ( and speed up dramaticly ) during runtime. It''s always a bad idea to call update from update or sleep during a graphics operation FYI.

Look at game frame 4 java and the other posts here and at javagaming.org . Your best bet is through the use of sleep.

What you probably want to do is implement Runnable in your class and in your public void run() method do a while( game ) { Thread.sleep(); update(this.getGraphics()); } ( this psudocode being simplified for postings sake ). Then you just need to do a Thread animator = new Thread(this); animator.start(); to kick it off. If that didn''t make sense you need to go to java.sun.com and read some more of the threading documentation.

Share this post


Link to post
Share on other sites
Like snowmoon, I don't really understand the meaning of your loop. Nor the meaning of the //loadimage comment. What is this loop for? just for spending time? or didn't we understood what you meant?
and the //loadimage: is it code that represent loading an image into memory? do you really load the same image 500 times into memory ?!? or is it more a displayImage or 'imageToBeDisplayed'?

Anyway, if the loop is so slow, 1'30 like you said, it's not because of your p133, nor the code, it's because you load 500 images into memory (if it is what you do)!

despite of this, i agree with snowmoon that it's not a nice way to do it.
A very common way to organize a "game" is something like this:

1. Load all stuff in memory
2. Start main loop
{
3. Store time
4. (React to input) (*)
5. Update all elements of the game
6. Draw all things to the backBuffer
7. Flip
8. Empty loop until time > previously stored time + delay
}
9. Clear everything


* The input can be managed on many very different ways, it can be a listener or you can ask it for the device staus depending how you manage it. So, it can be managed completely different than on the list above.

the (3,8) act as a timer to keep game speed and frame rate constant.

cheers

[edited by - misterX on July 27, 2002 7:30:13 PM]

Share this post


Link to post
Share on other sites
Actually switch 7,8 so that your akways flipping you backbuffer at a steady rate. With the loop as is you would be showing your back buffer whenever you got finished with your drawing code and that could lead to judder.

Share this post


Link to post
Share on other sites
Smart thing, flipping the buffer when the next frame starts... keeps it much more regular (although completley unnoticable at high frame rates). Just smart ;-)

-Phil Crosby
http://www.graphics-design.com

Share this post


Link to post
Share on other sites