Different kind of game loops in java -


i have searched around , come across different kind of game loops. have used

screen.java extends jframe, adds listeneres in 2 diffrent .java 1 keyboard, 1 mouse.

public void draw(graphics g){     graphics2d g2 = (graphics2d)g;     //everything render @ screen goes here, or pass along g2 them can rendered. such level.draw(g2);     repaint(); }  @override public void paint(graphics g){     image offscreenbuffer = createimage(getwidth(), getheight());     draw(offscreenbuffer.getgraphics());     g.drawimage(offscreenbuffer, 0, 0, null); } 

and in main have normal runnable loop sleep

@override public void run(){     while(true){         //all update code goes here.         try{ thread.sleep(1); }catch(exception e){}     } } 

but have seen people recommending using timer, semaphore, , timertask create run loop takes care of input, update, , lastly reneder. wounder method best? seams if use timertask's create loop render game seams need lock fps.

for have seen , understood can use timer , timertask schedule game updates. else don't how need change code much, or missing here?

for games i'd recommend time calculate fps. it's fps:

private void run() {     long = system.currenttimemillis();     long last = 0;     int fps = 60;     while(true)     {         if(now-last >= 1000/fps)         {             //do stuff              update();             render();             system.out.println("test");             last = now;         }         = system.currenttimemillis();      } } 

you add things here instead of while(true) use boolean stop , start game, maybe if use different gamestates. update within loop , render possible. can create update(float elapsed) instead of update() method, can update positions , other stuff consistent.

also @ question: https://gamedev.stackexchange.com/questions/59181/proper-method-to-update-and-draw-from-game-loop


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -