Game Development Reference
If you try this now, even though the player sprite never moves, it looks as if the player
is walking about the tilemap world. Perfect!
Well, not quite. If you move over mountains and walls and trees and buildings, the
player sprite is always drawn in front of them.
Enabling the Player to Move Behind Tiles
To allow the player to be partially hidden by object tiles in front of him, such as build-
ings, walls, trees, and whatnot, you have to change his vertexZ value as he moves
around on the map. At the start of this chapter, when you created the Objects layer in
Tiled, you gave it a property named cc_vertexz and set it to automatic. This in-
structed cocos2d to assign consecutive vertexZ values to the tiles in that layer. Fig-
ure 11-18 shows you which vertexZ values the tiles are assigned in a tilemap that's
50x50 tiles in size. This is different from the tile indices shown in Figure 11-14 be-
cause the vertexZ values increase in both X and Y directions. You could say that
vertexZ values decrease with each horizontal row of the tilemap .
This is reflected in code by the updateVertexZ method added to the Player
-(void) updateVertexZ:(CGPoint)tilePos tileMap:(CCTMXTiledMap*)tileMap
float lowestZ = −(tileMap.mapSize.width + tileMap.mapSize.height);
float currentZ = tilePos.x + tilePos.y;
self.vertexZ = lowestZ + currentZ - 1.5f;