Game Development Reference
In-Depth Information
part, you need to divide by the pointHeight and then cast down to an int value to
get the tile's y coordinate.
Before the coordinates are returned, they're clipped to valid coordinates within the
tilemap width and size. The fminf and fmaxf functions return whichever of the two
values is smaller or larger respectively; used in combination they clip the coordinates
to within 0 and tilemap size or width minus 1. This is far shorter than using a series of
if statements.
Working with the Object Layer
The orthogonal.tmx tilemap I've created as an example tilemap for this chapter
also contains an object layer, fittingly named ObjectLayer . You can create object
layers in Tiled by choosing Layer Add Object Layer. Then you can click inside the
tilemap and draw rectangles. I think the name object layer is a bit unfortunate and mis-
leading, because most games will use these rectangles as points of interest and trigger
areas, and not as actual objects.
In the Tilemap01 project, I've added a bit more code to the ccTouchesBegan meth-
od to interact with the object layer. Listing 10-4 shows the relevant part of the code,
which follows directly after the isWater check:
Listing 10-4 . Detecting If a Touch Was Inside an ObjectLayer Rectangle
// Check if the touch was within one of the rectangle objects
CCTMXObjectGroup* objectLayer = [tileMap objectGroupNamed:@"ObjectLayer"];
BOOL isTouchInRectangle = NO;
int numObjects = objectLayer.objects.count;
for (int i = 0; i < numObjects; i++)
{
NSDictionary* properties = [objectLayer.objects objectAtIndex:i];
CGRect rect = [self getRectFromObjectProperties:properties tileMap:tileMap];
if (CGRectContainsPoint(rect, touchLocation))
{
isTouchInRectangle = YES;

Custom Search