Advertisement

[java] Screen updates

Started by April 06, 2001 07:38 AM
18 comments, last by Dogbert 23 years, 9 months ago
I am developing a simple 2D, top down multiplayer tank game. It is an applet that connects to a central server. My problem is that when running under unix the canvas that the game is not being properly updated on the screen. I have a thread running which repaints the canvas at 20 frames per second. The paint method is being correctly called but the actual screen is only updated for about the first 3 seconds, then it doesn''t update for about 20-30 seconds then it updates for about 3 seconds again and so on. I thought this might be a problem with trying to repaint the screen too often, but I have tried as low as 5 frames per second but I''ve had no luck. The strangest thing (to me anyway) is that it works fine on my Windows 98 machine (800mhz athlon) but when it is run on unix (solaris) I get this problem. Has anyone got any ideas? Any help would be greatly appreciated. Thanks
We need to see some code samples. Or a more thourough explanation of how you are coding this thing. Also, VM versions on solaris and windows.
Advertisement
What are you doing between repaints? It sounds like the GUI thread is getting starved, make sure you are setting your main thread to sleep (or at least yield it) for a while. Usually under windows you can get away without that kind of thing (sometimes), but on other systems it just won''t work.

Alternatively set the main thread (non GUI) to a lower priority, which may (paradoxically) make redrawing seem quicker.

John
I have another thread that handles the movement of the tank. It moves the tank forward at the user defined speed at the same rate as the screen is updated (20 times a second) and detects any collisions that occur.

I haven''t touched the priority of any of the threads.
maybe you dont process the operating messages in your game code, it will help the OS to give some cpu time to the other threads

good luck

cyberg
cyberg- cyberg_coder@hotmail.com- http://members.xoom.com/cybergsoft
ha and why the hell are you doing all this stuff with multi threads when you can do that properly in just one thread, much more easy to debug, manage and code!!!

cyberg
cyberg- cyberg_coder@hotmail.com- http://members.xoom.com/cybergsoft
Advertisement
You''re always multithreading in java, as long as you have some sort of GUI. Well as long as you''re doing some sort of game type thing.

Anyway, what do you do to make the tank only update 20 times a second? I presume you send the thread to sleep in between moving the tank?
Try setting the tank moving thread to MIN_PRIORITY, just do:

Thread.currentThread().setPriority( Thread.MIN_PRIORITY );

at the top of the threads run method. That will give the GUI more processing for dealing with repaint() calls (which it might otherwise effectively ignore).

John
I am using three threads. One handles the updating the canvas that displays the game, one that handles the movement of the tank and another that receives messages from the server regarding the movement of the other players. It is for a university course I''m doing so I have to keep the code clean and object orientated.

Thanks for the help everyone, I will let you know how it goes.
Maybe you have a synchronization problem? (If setting the thread priority doesn''t work).

Someone mentions using only the main thread that the applet is running in instead of having multiple thread. I think that would be a good idea.

Different operating systems implement multithreading differently at the system level, therefore it would be a good idea to restrict the number of thread if you want your game it be cross-platform.
Also, strange this is I am also developing a top down 2d tank game that will be multiplayer with a central server. hehehe

Maybe we should get together and team up or something.

It''s still in the prototype phase and you cannot connect to the server yet (I need somewhere to host it).

http://www.fundi.net/byron/scrollengine/index.html

This topic is closed to new replies.

Advertisement