Game Development Reference
In-Depth Information
The code in ccTouchesBegan then simply checks whether the touch location is
contained in the rect via CGRectContainsPoint . If it is, the
isTouchInRectangle flag is set to true , and the for loop is aborted by using
the break statement. There's no need to check another rectangle for containing the
touch location. At the end of ccTouchesBegan , the isTouchInRectangle flag
is then used to decide whether to play a particle effect at the touch location. So, this
code is creating an explosion particle effect whenever you touch inside a rectangle:
if (isTouchOnWater)
[[SimpleAudioEngine sharedEngine] playEffect:@"alien-sfx.caf"];
else if (isTouchInRectangle)
CCParticleSystem* system = [CCParticleSystemQuad particleWithFile:←
system.autoRemoveOnFinish = YES;
system.position = touchLocation;
[self addChild:system z:1];
Drawing the Object Layer Rectangles
When you run the topic's Tilemap01 project, you'll notice that the object layer rect-
angles are drawn over the tilemap, as shown in Figure 10-12 . This isn't a standard fea-
ture of tilemaps or object layers. Instead, the rectangles are drawn using OpenGL ES
code. Every CCNode has a -(void) draw method that you can override to add cus-
tom OpenGL ES code. I tend to use this a lot to debug my code visually by drawing
lines, circles, and rectangles that may be used for collision and distance tests, among
other things. In this case, actually seeing where the object layer areas are is very useful.
Visualizing such information beats looking up and comparing coordinates in the debug-
ger. Our minds are much better at assessing visual information than they are at compar-
ing and calculating numbers. Use this fact to your advantage!
Search Nedrilad ::

Custom Search