Game Development Reference
In-Depth Information
NSAssert(layer ! = nil, @"Collisions layer not found!");
BOOL isBlocked = NO;
unsigned int tileGID = [layer tileGIDAt:tilePos];
if (tileGID > 0)
{
NSDictionary* tileProperties = [tileMap propertiesForGID:tileGID];
id blocks_movement = [tileProperties objectForKey:@"blocks_movement"];
isBlocked = (blocks_movement ! = nil);
}
return isBlocked;
}
The code first tries to get a tile at the given tile coordinate from the Collisions layer. If
there is no tile there, the tileGID will be 0, and you can safely assume that this tile is
not blocked. But if there is a valid tileGID at the tilePos coordinate, the
tileMap is queried for the properties of the tile, which returns an NSDictionary
object. If the dictionary's objectForKey method returns a valid object for the key
named blocks_movement , the tile is blocked.
The place to check for collision is in the update method, as shown in Listing 11-11 .
Listing 11-11. Checking for Collision in the update Method
-(void) update:(ccTime)delta
{
...
// if the tilemap is currently being moved, wait until it's done moving
if ([tileMap numberOfRunningActions] == 0)
{
if (currentMoveDirection ! = MoveDirectionNone)
{
CGPoint tilePos = [self tilePosFromLocation:screenCenter tileMap:tileMap];
CGPoint offset = moveOffsets[currentMoveDirection];
tilePos = CGPointMake(tilePos.x + offset.x, tilePos.y + offset.y);
 
 
Search Nedrilad ::




Custom Search