java - how to slow down one method without affecting the rest of the program? -


i'm making game in java , want create character moves randomly. 1 made spastic. want add delay between random numbers generated. i'm beginner don't judge code lol

public class monster extends entity{  private world world;  image monster;  public monster(int x, int y, world world) {         super(x, y, world);         w = 32;         h = 32;         this.world = world; }  public void render(gamecontainer gc, graphics g) throws slickexception{         super.render(gc, g);         monster = new image("gfx/world/monster.png");         g.drawimage(monster, x, y); }  public void update(gamecontainer gc, int delta) throws slickexception{     super.update(gc, delta);     random move = new random();     int number;      for(int counter=1; counter<=1;counter++){         number = move.nextint(4);          system.out.println(number);           if(number == 0){             setdy(-1);         }else if(number == 1){             setdx(-1);         }else if(number == 2){             setdy(5);            }else if(number == 3){             setdx(5);         }else{             setdx(0);             setdy(0);          }     } } } 

this common technique used on games have different update , render rate. have (examples in pseudo code):

1 - initialize time variable - datetime lastupdate = new datetime();

every time enter in loop:

2 - check if time has passed - lastupdate.haspassed(x_time, new datetime());

3 - if time has passed (last line true) lastupdate = new datetime();

4 - else return


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -