2

I'm trying to make a basic 2D game using Slick2D with the Tiled Map Editor. I have figured out how to do basic collision detection using TileProperties but I'm not sure how Objects work with the map editor. I'm trying to do two things (if they're possible): more precise polygon collision detection and game items on the map that the player can pick up.

The problem is that I don't know how to check for objects. I've looked in the Slick javadoc and I saw some methods that take int ObjectId and ObjectGroup as parameters but I'm not sure how they can be found. Could someone please explain? Even if I know how to check Objects, how would I scan the whole map for, say, "item" objects and do things with it, like render an image at that position?

Any help will be greatly appreciated.

Edit: I think I now know how to use objects but I still don't know how to get the objectID and objectGroupID. Could someone please explain ho to get the IDs either from Tiled or with Slick?

Rayexar
  • 33
  • 1
  • 5

1 Answers1

1

Check out the java doc http://slick.ninjacave.com/javadoc/ and object TiledMap.

The two methods you need here are: getObjectGroupCount() & getObjectCount(int groupid)

the getObjectGroupCount() method is going to return the total number of layers in your tiled map which are object layers, or rather the identifiers for each object layer.

the getObjectCount(int groupid) is going to return the total number of objects on any given layer, or the amount of objects within an object group.

From here you have the total number of layers in your map and the total number of objects on each layer, so you know how many times you need to need to loop in order to access each object by it's index, first it's group id and it's object id

I don't see a way to search by name, if somebody else does then please correct me. Otherwise I suggest reading this array when loading the map. If there are any object ID's that you need to call dynamically within the main game loop (while it's drawing) I would hold the ID's somewhere that you can use to access the object easily later.

EDIT: I'll do a quick non-syntax checked or tested code to explain:

TiledMap aMap = new TiledMap("whatever.tmx");
int objectGroupCount = aMap.getObjectGroupCount();
for( int gi; gi < objectGroupCount; gi++ ) // gi = object group index
{
    int objectCount = aMap.getObjectCount(gi);
    for( int oi; oi < objectCount; oi++ ) // oi = object index
    {
        System.out.println( aMap.getObjectName(gi, oi) );
        System.out.println( aMap.getObjectProperty(gi, oi, "somepropertyname", "" ) );
    }
}
Bryan Abrams
  • 327
  • 1
  • 8