Game Development Reference
In-Depth Information
The lowest vertexZ value is simply the sum of the map size width and height in the
negative. Likewise, you can get the difference of any tile coordinate in the tilemap to
the lowest vertexZ value, which is the tile at position 0, 0. It's the sum of the X and
Z coordinates of that position. For example, the tile at position 2, 2 is 2 + 2 = 4 less
than the lowest vertexZ value. If you add the two, you get −100 + 4 = −96. Since the
player sprite is added to the TileMapLayer after the tilemap, it will render on top of
tiles with the same vertexZ value. Because of this, I also subtract 1.5 so that the end
result is a vertexZ value of −96.5 if the player is standing on the tile coordinate 2, 2.
That also places the player behind any object on the same tile. If you want your player
to be in front of any object on the same tile, you should subtract 1.0f or less.
To make this code work, you also have to define the updateVertexZ method in the
Player class's interface:
@interface Player : CCSprite
{
}
+(id) player;
-(void) updateVertexZ:(CGPoint)tilePos tileMap:(CCTMXTiledMap*)tileMap;
@end
And then you should call the updateVertexZ method every time the tilemap is
moved, which is done in the ccTouchesBegan method of the TileMapLayer
class:
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CCNode* node = [self getChildByTag:TileMapNode];
NSAssert([node isKindOfClass:[CCTMXTiledMap class]], @"not a CCTMXTiledMap");
CCTMXTiledMap* tileMap = (CCTMXTiledMap*)node;
CGPoint touchLocation = [self locationFromTouches:touches];
CGPoint tilePos = [self tilePosFromLocation:touchLocation tileMap:tileMap];
[self centerTileMapOnTileCoord:tilePos tileMap:tileMap];
// update the player's vertexZ