Previous Tutorial
Next Tutorial
Posted on: 17-11-2012

Gravity Platformer Tutorial #9 : Loading Objects

The zip with the source for this tutorial can be downloaded here

One of the features of Tiled is the ability to add objects to map files, which allows us to load the objectives with the map.
  1. Lets start off by opening up Tiled and our map and adding a new layer.

    This new layer will be an object layer called "ObjectLayer", you can add it the same as you do with normal layers.

  2. Before we add any objects to this layer, we will have to define a type. Don't forget to double click and add a colour that identifies the object in the editor.

    The main reason why we define the types is because of their colour identification in the editor, in the game we will load the objects based on their name, not on their type.

  3. Now that we have defined an object type, we will add a new object.

    Simply click on the insert object button (or shortcut O) and then click somewhere in the world

  4. Now you want to right click on your object and set the name of the object to "Objective" and select our previous defined type "Objective". The X and Y here are in tiles, so 7 tiles will mean 7*32 pixels. The width and height are not relevant here, but lets set them to 1 anyway.

  5. Your map should look somewhat like this now:

  6. And now for the code changes, there is not that much too it.

    1. private void loadObjects() throws SlickException{
    3. //we only have one layer defined, slick2d does not support getting an object layer by name but that does not matter to much for our game
    4. int objectAmount = map.getObjectCount(0);
    6. //the objects are loaded into an array, again retrieval by name is not supported
    7. for(int i = 0; i < objectAmount; i++){
    8. switch(map.getObjectName(0, i)){
    9. case "Objective":
    10. addLevelObject(new Objective(map.getObjectX(0, i),map.getObjectY(0, i)));
    11. break;
    12. }
    13. }
    15. }

    As you can see I defined an additional method for loading objects in the Level class. But as you see I encountered a problem with the Slick2D framework here.
    The loading of objects does not work the same as it does with tiles, it should be, but unfortunately it doesn't.

    Luckily it isn't that big a deal in our game, as we will only have one object layer. And all the different object layers get mashed into an array which we can just read out of.

    The same goes for the objects in the layer itself, but as we can have more than one object in our layer we will have to cycle through them and create appropriate objects and add them to our level.

  7. As our last step, the removal of our hardcoded objects in levelstate.

Closing Notes

You might wonder why I didn't include our player in the objects that we load as well. This is because our player is not a property of the level itself, but of the game itself. The player is an object that carries over from level to level, but the objects inside levels will stay inside the levels.

Categories: Game Development, Java, Tutorial


Alex said: (08-12-2014)
My program throws an error when it tries to load the map if it has an objectlayer. How can I fix this?

What is the name of the website? (to counter the spam)