Game Development Reference
In-Depth Information
Just as before, I iterate over all object layer objects and get their properties from
NSDictionary to get the CGRect of that object, which is then used in the
ccDrawRect and ccDrawSolidRect methods. Unfortunately, these methods
don't take a CGRect as input but an origin and destination CGPoint . Therefore, you
have to create the destination point manually by adding the corresponding sizes to the
origin coordinates.
Note that the draw method is enclosed in #ifdef DEBUG and #endif statements.
This means that the object layer rectangles won't be drawn in release builds, because
they're only needed for debugging and illustration purposes—the end user should nev-
er see them.
Scrolling the Tilemap
The best part comes last: scrolling. It's actually straightforward because you only need
to move the CCTMXTiledMap . In the Tilemap01 project, I've added the call to the
centerTileMapOnTileCoord method in ccTouchesBegan right after obtain-
ing the tile coordinates of the touch:
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
...
// Get the position in tile coordinates from the touch location
CGPoint touchLocation = [self locationFromTouches:touches];
CGPoint tilePos = [self tilePosFromLocation:touchLocation tileMap:tileMap];
// Move tilemap so that the touched tile is at the center of the screen
[self centerTileMapOnTileCoord:tilePos tileMap:tileMap];
...
}
Listing 10-6 shows the centerTileMapOnTileCoord method, which moves the
tilemap so that the touched tile is at the center of the screen. It also stops the tilemap
from scrolling farther if any tilemap border already aligns with the screen edge.
Listing 10-6 . Centering the Tilemap on a Tile Coordinate
 
 
Search Nedrilad ::




Custom Search