Game Development Reference
In-Depth Information
Because object layers are a different kind of layer, you can't get them via the layer-
Named method of the tilemap. The object layer in cocos2d is the class CCTMXOb-
jectGroup — another unfortunate naming mishap, because Tiled calls it an object
layer , not an object group . In any case, you can get the CCTMXObjectGroup for the
object layer named simply ObjectLayer by using the tilemap's ob-
jectGroupNamed method and specifying the object layer's name as defined in
Next, I iterate over the objectLayer.objects NSMutableArray , which con-
tains a list of NSDictionary items. Sound familiar? Yes, these are the same
NSDictionary properties returned by the tilemap's propertiesForGID method,
as shown earlier—except that the contents of these NSDictionary items are given
by Tiled and aren't user-editable. They simply contain the coordinates for each rect-
angle. The method getRectFromObjectProperties returns the rectangle:
-(CGRect) getRectFromObjectProperties:(NSDictionary*)dict tileMap:(CCTMXTiledMap*)tileMap
float x, y, width, height;
x = [[dict valueForKey:@"x"] floatValue] + tileMap.position.x;
y = [[dict valueForKey:@"y"] floatValue] + tileMap.position.y;
width = [[dict valueForKey:@"width"] floatValue];
height = [[dict valueForKey:@"height"] floatValue];
return CGRectMake(x, y, width, height);
The keys x , y , width , and height are set by Tiled. I simply retrieve them from the
NSDictionary via valueForKey and use the floatValue method to convert
the values from NSString to actual floating-point numbers. The x and y values need
to be offset with the tileMap 's position, because the rectangles need to be moving
along with the tilemap. At the end, a CGRect is returned by calling the CGRectMake
convenience method.
Search Nedrilad ::

Custom Search