Tile/grid movement in Java (2d game) -


i started game-making. did follow tutorial on youtube codenmore, not pixel-based movement system. wanted make grid-based movement system, couldn't work out how can this. not advanced enough read some1 else code , understand how works, that's why ask guys!

my tiles 64x64, change x value 64. doesn't me because character starts teleport 64 pixels @ once instead of moving smoothly pixel pixel 64 pixels (tile size). tried using for-loops, timers, , thread.sleep, didn't me.

//activate both of movement methods.  public void move() throws interruptedexception {      if (!checkentitycollisions(xmove, 0f)) {         movex();     }     if (!checkentitycollisions(0f, ymove)) {         movey();     }      system.out.println("pos x: " + x + "\n pos y: " + y);  }  //movement method  public void movex() throws interruptedexception {     timer += system.currenttimemillis() - lasttime;     lasttime = system.currenttimemillis();      if (xmove > 0) { // moving right          int tx = (int) (x + xmove + bounds.x + bounds.width) / tile.tilewidth;          //checks collisions         if (!collisionwithtile(tx, (int) (y + bounds.y) / tile.tileheight)                 && !collisionwithtile(tx, (int) (y + bounds.y + bounds.height) / tile.tileheight)) {              //tilesize = 64             x += tilesize;          } else {             x = tx * tile.tilewidth - bounds.x - bounds.width - 1;         }      } else if (xmove < 0) { //moving left          int tx = (int) (x + xmove + bounds.x) / tile.tilewidth;          if (!collisionwithtile(tx, (int) (y + bounds.y) / tile.tileheight)                 && !collisionwithtile(tx, (int) (y + bounds.y + bounds.height) / tile.tileheight)) {              x -= tilesize;          } else {             x = tx * tile.tilewidth + tile.tilewidth - bounds.x;         }     }  } 

//graphics code:

private display display; private int width, height;  public string title; public boolean running = false;  private thread thread;  private bufferstrategy bs; private graphics g;  //states private state gamestate; private state menustate;  //input private keymanager keymanager;  //camera private gamecamera gamecamera;  //handler private handler handler;  //game engine public game(string title, int width, int height) {      this.height = height;     this.width = width;     this.title = title;     keymanager = new keymanager();  }  public void init() {      display = new display(title, width, height);     display.getframe().addkeylistener(keymanager);     assets.init();      gamecamera = new gamecamera(this, 0, 0);     handler = new handler(this);      gamestate = new gamestate(handler);     menustate = new menustate(handler);     state.setstate(gamestate); }  public void tick() {      keymanager.tick();      if (state.getstate() != null) {         state.getstate().tick();     } }  public void render() {      bs = display.getcanvas().getbufferstrategy();     if (bs == null) {         display.getcanvas().createbufferstrategy(3);         return;     }     g = bs.getdrawgraphics();      //clear screen     g.clearrect(0, 0, width, width);      //draw here!     if (state.getstate() != null) {         state.getstate().render(g);     }      //end drawing     bs.show();     g.dispose(); }  public void run() {     init();      int fps = 60;     double timepertick = 1000000000 / fps;     double delta = 0;     long now;     long lasttime = system.nanotime();      while (running) {          = system.nanotime();         delta += (now - lasttime) / timepertick;         lasttime = now;          if (delta >= 1) {             tick();             render();             delta--;         }      }     stop(); }  public int getwidth() {     return width; }  public void setwidth(int width) {     this.width = width; }  public int getheight() {     return height; }  public void setheight(int height) {     this.height = height; }  public keymanager getkeymanager() {     return keymanager; }  public gamecamera getgamecamera() {     return gamecamera; }  public synchronized void start() {      if (running) {         return;     }     running = true;     thread = new thread(this);     thread.start(); }  public synchronized void stop() {     if (!running) {         return;     }     running = false;     try {         thread.join();     } catch (interruptedexception e) {         e.printstacktrace();     } } 

}


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -