Game Development Reference
In-Depth Information
-(void) centerTileMapOnTileCoord:(CGPoint)tilePos tileMap:(CCTMXTiledMap*)tileMap
{
// center tilemap on the given tile pos
CGSize screenSize = [CCDirector sharedDirector].winSize;
CGPoint screenCenter = CGPointMake(screenSize.width * 0.5f, ←
screenSize.height * 0.5f);
// tile coordinates are counted from upper left corner, this maps coordinates to lower left corner
tilePos.y = (tileMap.mapSize.height - 1) - tilePos.y;
// scaling tileSize to Retina display size
float pointWidth = tileMap.tileSize.width / CC_CONTENT_SCALE_FACTOR();
float pointHeight = tileMap.tileSize.height / CC_CONTENT_SCALE_FACTOR();
// point is now at lower left corner of the screen
CGPoint scrollPosition = CGPointMake(−(tilePos.x * pointWidth), ←
-(tilePos.y * pointHeight));
// offset point to center of screen and center of tile
scrollPosition.x + = screenCenter.x - pointWidth * 0.5f;
scrollPosition.y += screenCenter.y - pointHeight * 0.5f;
// make sure tilemap scrolling stops at the tilemap borders
scrollPosition.x = MIN(scrollPosition.x, 0);
scrollPosition.x = MAX(scrollPosition.x, -screenSize.width);
scrollPosition.y = MIN(scrollPosition.y, 0);
scrollPosition.y = MAX(scrollPosition.y, -screenSize.height);
CCLOG(@"tilePos: (%i, %i) moveTo: (%.0f, %.0f)",
(int)tilePos.x, (int)tilePos.y, scrollPosition.x, scrollPosition.y);
CCAction* move = [CCMoveTo actionWithDuration:0.2f position:scrollPosition];
[tileMap stopAllActions];
[tileMap runAction:move];
}
After obtaining the center position of the screen, I modify the tilePos y coordinate,
because tilemap coordinates are counted from top to bottom (see Figure 10-11 ), and
Search Nedrilad ::




Custom Search