标签:
(一)Scrolling the Isometric Tilemap
With the tilePosFromLocation method updated to work with isometric tilemaps, the
Tilemap06 project continues by implementing isometric tilemap scrolling, using the tile
coordinates returned from the tilePosFromLocation method. Just as in the orthogonal
tilemap project, this is done using the centerTileMapOnTileCoord method, shown in
Listing 11–3. Scrolling the Screen to Center on a Specific Tile Coordinate
//屏幕中心(中央)定位到某一瓦片
//参数tilePos,为瓦片坐标;参数tileMap为地图
-(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); // get the ground layer CCTMXLayer* layer = [tileMap layerNamed:@"Ground"]; NSAssert(layer != nil, @"Ground layer not found!"); // internally tile Y coordinates are off by 1 tilePos.y -= 1; // get the pixel coordinates for a tile at these coordinates CGPoint scrollPosition = [layer positionAt:tilePos]; // negate the position to account for scrolling scrollPosition = ccpMult(scrollPosition, -1); // add offset to screen center scrollPosition = ccpAdd(scrollPosition, screenCenter); // move the tilemap CCAction* move = [CCMoveTo actionWithDuration:0.2f position:scrollPosition]; [tileMap stopAllActions]; [tileMap runAction:move]; }
First, the screen center position is determined as before. Then I want to use the convenience method of the layer, positionAt, which returns a screen position for a tile coordinate. To do so, I get the Ground layer and assert that it exists. It doesn’t matter
which layer you use, as long as all layers use same size tiles.
Before calling the positionAt method, I have to subtract 1 from the tile Y coordinate to fix a persistent offset problem. Seasoned programmers may be worried that using a tile
Y coordinate of 0 and subtracting 1 from it could lead to an invalid index and thus a
disastrous crash. But in this case the positionAt method doesn’t use the tile
coordinates as indices, and it works with any tile coordinate, even negative ones.
The positionAt method returns the pixel position of the given tile coordinate within the tilemap and stores it in the scrollPosition variable. This method isn’t specific to isometric tilemaps; it works for all tilemap types: orthogonal, isometric, and hexagonal.
Internally, cocos2d checks which type of tilemap is currently being used and then uses the appropriate calculation, since they differ in profound ways. If you are interested in
the specific implementation of each of these calculations, take a look at the methods positionForOrthoAt, positionForIsoAt, and positionForHexAt in the CCTMXLayer.m implementation file.
Because the tilemap may be scrolling, in which case it will have a negative position, the scrollPosition is multiplied by -1, negating it.
After that I just add the screenCenter position to it and I know where to scroll to.
The move action is the same as before and moves the tilemap so that the touched tile is centered on screen.
(二) This World Deserves a Better End(世界地图结尾修饰)
参见P251
Due to the diamond-shaped nature of isometric tilemaps, it’s inevitable that the scrolling tilemap will reveal parts outside of the tilemap, as shown in Figure 11–15. Indeed, the tilePosFromLocation method ensures that the returned tile coordinate is always within
bounds, so you can use that safely even if the player touches outside the tilemap.
But if you don’t want the player to see the end of your isometric tilemap world, you’ll have to use a trick.
In Tiled, use Map ? Resize Map… to bring up the Resize dialog shown in Figure 11–16. You need to add 10 tiles to each side
of this tilemap to completely fill the border. Depending on the tile size, you will have to
experiment to find the minimum number of tiles that need to be appended.
标签:
原文地址:http://www.cnblogs.com/xiaosafeimao/p/4994946.html