I'm trying to implement some collision detection to a test game i've been making. I've used TiledMap to create the map and set a property on one of the tiles to blocked=true This tile is then drawn on layer 0.
I then check to see if the tile exists in the direction the player is moving, using the following code
if (input.isKeyDown(Input.KEY_DOWN)) {
sprite = down;
sprite.update(delta);
int tileID = map.map.getTileId((int) x / map.map.getTileWidth(), (int) y / map.map.getTileHeight() + 1, 0);
String value = map.map.getTileProperty(tileID, "blocked", "false");
if (value.equals("true")) {
y += delta * 0.1f;
System.out.println("Tile ID: " + (int) (x / map.map.getTileWidth()) + ", " + (int) (y / map.map.getTileHeight() + 1) + " Try to walk down. Tile value below the player is:" + value);
}
}
This is repeated for each direction.
The problem I'm running into is it's picking up the blocked property for incorrect tiles You can understand better with this video . The yellow tiles are the collision/blocked tiles.
I think you have this issue because you update the graphics outside your if
statement
Try to move the ones below inside the if
:
sprite = down;
sprite.update(delta);
However I suppose one of these put the face of the sprite down and therefore should not be inside.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.