java - Convert class implements ActionListener into thread -


i have simple 2d game class looks like:

public class game extends jpanel implements actionlistener {     private timer timr;      public game(){         //other stuff         timr = new timer(10, this);         timr.start();      }     //other methods including actionlistener-related ones } 

and instead of using timer() timing want run game thread, how can , keep actionlistener functions?

don't tie ui other game components. need have separation of concerns. consider having class holds representation of things in game, game state. ui should concern drawing current game state. game should run in loop updates game state, renders ui.

class game() {    world world; //holds state of things in game   ui ui;   long time;   long elapsed; //number of ms since last update    maingameloop() {      time = system.currenttimeinmillis();      while (gamerunning()) {       elapsed = system.currenttimeinmillis() - time;       time = system.currenttimeinmillis();       world.update(elapsed); //updates game state       ui.render(world);      //draws game state screen     }    } } 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -