Game Development Reference
In-Depth Information
for (int i = 0; i < numObjects; i++)
NSDictionary* properties = [objectLayer.objects objectAtIndex:i];
CGRect rect = [self getRectFromObjectProperties:properties tileMap:tileMap];
CGPoint dest = CGPointMake(rect.origin.x + rect.size.width, ←
rect.origin.y + rect.size.height);
ccDrawRect(rect.origin, dest);
ccDrawSolidRect(rect.origin, dest, ccc4f(1, 0, 1, 0.3f));
// reset line width
First, I get the tilemap by its tag and then the CCTMXObjectGroup by using the ob-
jectGroupNamed method. I then set the line width to 2 pixels with the OpenGL ES
method glLineWidth . Multiplying with CC_CONTENT_SCALE_FACTOR() en-
sures that the line width scales to 4 pixels on Retina devices, because
CC_CONTENT_SCALE_FACTOR() returns 2.0f on Retina devices, otherwise 1.0
f . This affects line thickness and color of all subsequent lines drawn with OpenGL
ES—not just in the current method but possibly other nodes that use OpenGL ES code
for drawing (for example, any of the convenience methods for drawing lines, circles,
and polygons defined in cocos2d's CCDrawingPrimitives.h header file). That's
why I reset glLineWidth after I'm done drawing. It's good style in OpenGL code to
leave its state like you found it; otherwise, it might alter the way other draw code pro-
duces its output. OpenGL is a state machine, so every setting you change is re-
membered and may affect subsequent drawing methods. To avoid this, any OpenGL
settings you change should be set back to a safe default after you're done drawing.
Note Code inside the -(void) draw method is always drawn at a z-order of
0. It's also drawn before all other nodes at z-order 0, which means that any
OpenGL ES code will be overdrawn by other nodes if they're also at z-order 0.
In the case of the object layer draw code, I had to add the tileMap at a z-or-
der of −1 for the rectangles to be drawn over the tilemap.
Search Nedrilad ::

Custom Search