I\'m struggling with implementing a collision detection system through the tiledmap. I have a 2d \"pokemon style\" game that has a tiled map rendered. Specifically, I have a \
First of all your Player
should probably not extend Sprite
, because your player is usually much more than a Sprite
. It probably consists of several sprites or even Animations
. Keep a sprite as a property of the player.
The question itself has already been adressed several times. You usually need the following steps:
In code this might look a bit like this:
int objectLayerId = 5;
TiledMapTileLayer collisionObjectLayer = (TiledMapTileLayer)map.getLayers().get(objectLayerId);
MapObjects objects = collisionObjectLayer.getObjects();
// there are several other types, Rectangle is probably the most common one
for (RectangleMapObject rectangleObject : objects.getByType(RectangleMapObject.class)) {
Rectangle rectangle = rectangleObject.getRectangle();
if (Intersector.overlaps(rectangle, player.getRectangle()) {
// collision happened
}
}
Some more links which you might be interested in: