Game Development Reference
In-Depth Information
area, the new tile coordinate is run through the ensureTilePosIsWithinBounds
method. It's the same code we used before to keep the tile coordinate within the play-
able area but refactored into a separate method to avoid duplicating this code. Lastly,
the centerTileMapOnTileCoord method is called to move and center the screen
on the desired tile coordinate, which also adds the move action.
The methods ensureTilePosIsWithinBounds and floatTilePosFromLocation have been
refactored to be reusable. Previously they were merged into the tilePosFromLocation
method. The refactored methods still perform the same functions but individual aspects
such as adjusting the tile position to be in the playable area can now be used by other
code too. Following are the refactored methods:
-(CGPoint) ensureTilePosIsWithinBounds:(CGPoint)tilePos
{
tilePos.x = MAX(playableAreaMin.x, tilePos.x);
tilePos.x = MIN(playableAreaMax.x, tilePos.x);
tilePos.y = MAX(playableAreaMin.y, tilePos.y);
tilePos.y = MIN(playableAreaMax.y, tilePos.y);
return tilePos;
}
-(CGPoint) floatTilePosFromLocation:(CGPoint)location tileMap:(CCTMXTiledMap*)tileMap
{
CGPoint pos = ccpSub(location, tileMap.position);
float halfMapWidth = tileMap.mapSize.width * 0.5f;
float mapHeight = tileMap.mapSize.height;
float tileWidth = tileMap.tileSize.width / CC_CONTENT_SCALE_FACTOR();
float tileHeight = tileMap.tileSize.height / CC_CONTENT_SCALE_FACTOR();
CGPoint tilePosDiv = CGPointMake(pos.x / tileWidth, pos.y / tileHeight);
float mapHeightDiff = mapHeight - tilePosDiv.y;
// Cast to int makes sure that result is in whole numbers
float posX = (mapHeightDiff + tilePosDiv.x - halfMapWidth);
float posY = (mapHeightDiff - tilePosDiv.x + halfMapWidth);
return CGPointMake(posX, posY);
}
-(CGPoint) tilePosFromLocation:(CGPoint)location tileMap:(CCTMXTiledMap*)tileMap
Search Nedrilad ::




Custom Search