java - LibGDX Tiled Map Collision Detection with Property and Layers -


i building simple top down rpg game libgdx haven't found precise pointers on how detect collision on tiled map.

i have character class looks this

public class son{      spritebatch batch;     texture texture;     sprite sprite;      private tiledmaptilelayer collisionlayer;       public son(float x, float y, tiledmaptilelayer collisionlayer){         this.collisionlayer = collisionlayer;         batch = new spritebatch();         texture = new texture(gdx.files.internal("characters/xter1.png"));         sprite = new sprite(texture);         sprite.setx(x);         sprite.sety(y);      }      public void render(){          processkeys();         batch.begin();         sprite.draw(batch);         batch.end();      }       private void processkeys(){         if(gdx.input.istouched()){            //gdx.app.exit();             gdx.app.log("x touched", gdx.input.getx() + "");             gdx.app.log("y touched", gdx.input.gety() + "");              //480 840              //left             if(gdx.input.getx() >= 0 && gdx.input.getx() <= 80){                 if(gdx.graphics.getheight() - gdx.input.gety() >= gdx.graphics.getheight()/6 && gdx.graphics.getheight() - gdx.input.gety() <= gdx.graphics.getheight()/6 + 80){                     sprite.setposition(sprite.getx() - 10, sprite.gety());                 }             }              //right             if(gdx.input.getx() >= (int)(gdx.graphics.getwidth()/8.4) && gdx.input.getx() <= (int)(gdx.graphics.getwidth()/8.4) + 80){                 if(gdx.graphics.getheight() - gdx.input.gety() >= gdx.graphics.getheight()/6 && gdx.graphics.getheight() - gdx.input.gety() <= gdx.graphics.getheight()/6 + 80) {                     sprite.setposition(sprite.getx() + 10, sprite.gety());                 }             }               //up             if(gdx.input.getx() >= (int)(gdx.graphics.getwidth()/16.8) && gdx.input.getx() <= (int)(gdx.graphics.getwidth()/16.8) + 80){                 if(gdx.graphics.getheight() - gdx.input.gety() >= gdx.graphics.getheight()/3.2 && gdx.graphics.getheight() - gdx.input.gety() <= gdx.graphics.getheight()/3.2 + 80){                     sprite.setposition(sprite.getx(), sprite.gety() + 10);                 }             }             //down             if(gdx.input.getx() >= (int)(gdx.graphics.getwidth()/16.8) && gdx.input.getx() <= (int)(gdx.graphics.getwidth()/16.8) + 80){                 if(gdx.graphics.getheight() - gdx.input.gety() >= gdx.graphics.getheight()/48 && gdx.graphics.getheight() - gdx.input.gety() <= gdx.graphics.getheight()/48 + 80){                     sprite.setposition(sprite.getx(), sprite.gety() - 10);                  }             }         }     }  } 

this code changes location of sprite based on part of screen user pressing.

the class contains tiled map looks this

public class stageonescreen implements screen {     tiledmap tiledmap;     orthographiccamera camera;     fitviewport viewport;     tiledmaprenderer tiledmaprenderer;     stage stage;     controllerrenderer controllerrenderer;     son son;     private music music;      final float game_world_height = 100;     final float game_world_width = 50;      private tiledmap collisionmap;     private tiledmaptilelayer collisionlayer;       public stageonescreen(){         float aspectratio = (float) gdx.graphics.getheight() / (float) gdx.graphics.getwidth();          camera = new orthographiccamera(game_world_height * aspectratio, game_world_height);         camera.position.set(game_world_width/2, game_world_height/2, 0);         //viewport = new fitviewport(800, 400, camera);         camera.settoortho(false, 800, 400);         camera.update();           tiledmap = new tmxmaploader().load("stages/stage1.tmx");         tiledmaprenderer = new orthogonaltiledmaprenderer(tiledmap);          stage = new stage(new screenviewport());         stage.getviewport().update(gdx.graphics.getwidth(), gdx.graphics.getheight(), true);         gdx.input.setinputprocessor(stage);     }      @override     public void show() {        // playmusic();         controllerrenderer = new controllerrenderer();         son = new son(100, 100, collisionlayer);     }      @override     public void render(float delta) {         gdx.gl.glclearcolor(1, 0, 0, 1);         gdx.gl.glblendfunc(gl20.gl_blend_src_alpha, gl20.gl_one_minus_src_alpha);         gdx.gl.glclear(gl20.gl_color_buffer_bit);          camera.update();         tiledmaprenderer.setview(camera);          tiledmaprenderer.render();         controllerrenderer.render(camera);         son.render();     }      @override     public void resize(int width, int height) {      }      @override     public void pause() {      }      @override     public void resume() {      }      @override     public void hide() {      }      @override     public void dispose() {         controllerrenderer.dispose();      }      public void playmusic(){         // load music asset , check if loop state before playing         music =  soundasset.loadmusic(soundasset.music_name);         music.setlooping(true);         if (music.islooping())             music.play();     } } 

on tiled map rendering, have given tiles property "wall". use 2 layers. tiles don't want "son" character able move through on second layer. how stop character going through tiles?

if using tiledmap editor : http://www.mapeditor.org/, can set collision properties using tags editor , retreive value code.

when using libgdx small 2d game, had strategy pattern draw , move objects. on assumption set properties, read , logic. "on player move, if nextcase == wall , nextcase = currentcase"


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -