loops - (FIXED) My java game runs in slow motion on different computers -


i developing game, , wanted test on different computer check if resolutions ok , stuff noticed 1 big problem, game runs in slow motion reason... not laggy, slow motion.. game loop temporary:

while(gameisrunnin){      dostuff();      thread.sleep(1000/60); 

but after test, i've tried check how time take dostuff(); code , tested this:

while(gameisrunnin){      long startt = system.currenttimemillis();      dostuff();      long stopt = system.currenttimemillis();      system.out.println(stopt-startt);      thread.sleep(1000/60); 

the result gives me 0, on both computers, (on 1 developing game runs in perfect speed, , on pc runs in slow motion.. tested nano time, gives me 50000-80000 on both computes (pretty same result. what's up? superman save me?

update: ok when run game on other computer not on full screen, it's runs fine, when on full screen, it's slowmotion update: looks superhero here, i've set displaymode refresh rate unknown, guess whole problem...

you've use called "delta time". means, measure how long takes 1 iteration of game loop , use number movements.

this because of different count of fps on different computers. instead of moving objects constant amount of pixels, you're defining speed , calculating actual size of movement dynamically.

short example:

public void gameloop() {     long initialtime = system.nanotime();      game.redraw();     game.update(system.nanotime() - initialtime); }  // inside class game public void update(long deltatime) {     someobject.movetoright(deltatime * speed); } 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -