首先我们要根据所需的地图制作两张tilemap地图,然后根据当前的position判断是目前是哪一张地图进行合理的显示。
附主要代码:
1 -(void) setViewPointCenter:(CGPoint)position 2 { 3 CGSize winSize = [[CCDirector sharedDirector] winSize]; 5 if (-1*self.position.x>((_tilemap.mapSize.width * _tilemap.tileSize.width)+_tilemap.position.x-winSize.width-1.0)) { 6 if ((-1*self.position.x>(_tilemap1.mapSize.width * _tilemap1.tileSize.width)+_tilemap1.position.x-winSize.width-1.0)) 7 { 8 _tilemap.position = ccp(_tilemap1.position.x+(_tilemap1.mapSize.width * _tilemap1.tileSize.width),0); 9 } 10 else { 11 CGPoint mapPosition = [self tilePosFromLocation:_spiderman.position tileMap:_tilemap1]; 12 int x = MAX(position.x, winSize.width / 2); 13 int y = MAX(position.y, winSize.height / 2); 14 x = MIN(x, (_tilemap1.mapSize.width * _tilemap1.tileSize.width)+_tilemap1.position.x 15 - winSize.width / 2); 16 y = MIN(y, (_tilemap1.mapSize.height * _tilemap1.tileSize.height) 17 - winSize.height/2); 18 CGPoint actualPosition = ccp(x, y); 19 20 CGPoint centerOfView = ccp(winSize.width/2, winSize.height/2); 21 CGPoint viewPoint = ccpSub(centerOfView, actualPosition); 22 self.position = viewPoint; 23 } 24 } 25 else { 26 CGPoint mapPosition = [self tilePosFromLocation:_spiderman.position tileMap:_tilemap]; 27 int x = MAX(position.x, winSize.width / 2); 28 int y = MAX(position.y, winSize.height / 2); 29 x = MIN(x, (_tilemap.mapSize.width * _tilemap.tileSize.width)+_tilemap.position.x 30 - winSize.width / 2); 31 y = MIN(y, (_tilemap.mapSize.height * _tilemap.tileSize.height) 32 - winSize.height/2); 33 CGPoint actualPosition = ccp(x, y); 35 CGPoint centerOfView = ccp(winSize.width/2, winSize.height/2); 36 CGPoint viewPoint = ccpSub(centerOfView, actualPosition); 37 self.position = viewPoint; 38 if((-1*self.position.x>(_tilemap.mapSize.width * _tilemap.tileSize.width)+_tilemap.position.x-winSize.width-1.0)) 39 { 40 _tilemap1.position = ccp(_tilemap.position.x+(_tilemap.mapSize.width * _tilemap.tileSize.width),0); 41 } 42 } 43 }
整个思想还是比较简单,就是根据目前的地图将第二章地图放到目前地图的后面紧跟,注意tilemap大小的计算:
_tilemap1.mapSize.width * _tilemap1.tileSize.width