1
\$\begingroup\$

The below 1st image is the example how I render tiled map with player, I can properly change the player z index, If I want to make the player below the box i just set the z index to 0. But the problem is the 2nd image below, I dont know how do I make the player above the box when the player is in the bottom then when in the top the player index should be less than the box.

enter image description here

\$\endgroup\$
3
  • \$\begingroup\$ To me it's not clear what you want. Is the red box supposed to be an obstacle and the top player behind it? If so, couldn't you just sort drawing by Y position from back to front? \$\endgroup\$
    – Mario
    Jul 24, 2016 at 7:10
  • \$\begingroup\$ yes it is an object, thats my problem the result is the player is looks like on top. \$\endgroup\$
    – ronscript
    Jul 24, 2016 at 7:27
  • \$\begingroup\$ the below image is the desired result \$\endgroup\$
    – ronscript
    Jul 24, 2016 at 7:28

2 Answers 2

1
\$\begingroup\$

Sort by z position. You will probably want to add an origin to every object as well to determine their position in the game world (ie the players feed, the center of where the box touches the ground, etc.).

There are some restrictions for what objects this will work for though. When the players z position becomes larger or smaller than that of an object he switches from in front of the object to behind it, or vice versa, so you want to make sure that the player can not be in a position where the switch becomes obvious. that means that if you draw a line horizontally left and right from the origin, to the far left and the far right of the objects collision geometry it should ideally stay completely within that geometry.

\$\endgroup\$
1
\$\begingroup\$

I see two solutions to this:

  • Make your player a part of the TiledMap by adding him to his own layer. This will automatically render him in the correct order like all of your other tiles.
  • Add an integer property to your tiles in whatever program you're using to create your maps named "zIndex" and then sort your tiles by this property and render them in the order of lowest to highest z-index.

For solution 1, in your editor, set up the layers to be something like this:

layers

Then the code should be something like this:

// Adding player to a TiledMapObjectLayer
// Create new MapObject to store player object in.
MapObject object = new MapObject();
// Put the player object in the properties of the map object.
object.getProperties().put("player", yourPlayerObject);
// Then add the object to whatever layer you wish.
yourTileMap.getLayers().get("playerLayer").getObjects().add(object);

For a more in-depth solution (with several alternatives) you can take a look at this resource.

\$\endgroup\$
1
  • \$\begingroup\$ let me try this one, I'll let you know if this will work for my system. \$\endgroup\$
    – ronscript
    Jul 25, 2016 at 19:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .