Making a game, timing problem
I'm about to make a game which uses swing. And when i do animation, on diffrent systems game executes at diffrent speeds. Here is how i solved it :
// private members
privatestaticint fps, frames, seconds;
// constructor
fps = 30;// assagning a default value
frames = 0;
seconds = System.currentTime();// or whatever it's called, i can't remember now
// main loop
frames++;// counting frames
if ( System.currentTime() - seconds > 1000 ){// if a second has passed
fps = frames;// assign fps
frames = 0;// restart value
seconds = System.currentTime();// update the seconds
}
That's how i calculate fps, which then i use in animation calculations. and so on.
I was just wondering is there a better solution to this ?
# 5
> Let's say i have a class named Game, which
> contains calculateData and drawStuff
> methods. How could I implement this using Timer in
> the public static void main(String[] args)
> function. Could someone please post an example ?
See reply #3.
# 7
> I allready saw it. But the problem is that i don't
> know when and how to call the right methods from the
> Game class.
Well, if you don't know, how could we? It's your game, you designed it.
Let the timer task call calculateData and drawStuff, then wait and repeat.
# 8
> I allready saw it. But the problem is that i don't
> know when and how to call the right methods from the
> Game class.
Your question doesn't make much sense. I think it would be a good idea to do a tutorial:
http://javaboutique.internet.com/tutorials/Java_Game_Programming/