用過cocos2d都知道Tiled編輯的2d方塊地圖tmx,在cocos2d中有本身能解析的庫,可是SpriteKit沒有,怎麼能作拼組地圖了。node
大神寫了一個:https://github.com/slycrel/JSTileMap git
定義地圖類:github
#import "JSTileMap.h" @interface MSTileMap : JSTileMap @property (nonatomic,strong) TMXLayer *wall; @property (nonatomic,strong) TMXLayer *road; @property (nonatomic,strong) TMXLayer *enemy; @property (nonatomic,strong) TMXLayer *item; @property (nonatomic,strong) TMXLayer *door; @property (nonatomic,strong) TMXLayer *other; @property (nonatomic,strong) TMXLayer *npc; @property (nonatomic,strong) TMXLayer *hero; -(id)initWithIndex:(NSInteger)index dot:(NSInteger)dot; @end
TMXLayer就是設計地圖時的圖層atom
-(id)initWithIndex:(NSInteger)index dot:(NSInteger)dot { self = [super initWithMapNamed:[NSString stringWithFormat:@"map%d_%d.tmx", index+1, dot+1]]; if (self) { _road = [self layerNamed:@"road"]; _item = [self layerNamed:@"item"]; _enemy = [self layerNamed:@"enemy"]; _door = [self layerNamed:@"door"]; _npc = [self layerNamed:@"npc"]; _other = [self layerNamed:@"other"]; _hero = [self layerNamed:@"hero"]; _wall = [self layerNamed:@"wall"]; } return self; }
定位地圖裏的某個元素設計
-(void)titledMapAnalytic { for (int x = 0; x <= self.mapSize.width; x++) { for (int y = 0; y <= self.mapSize.height; y++) { CGPoint towerLoc = CGPointMake(x, y); // 敵人經過座標得到gid int enemy_tileGid = [_enemy tileGidAt:towerLoc]; if (enemy_tileGid) { // 經過Gid得到方塊屬性,方塊的屬性在編輯地圖裏就已經寫好了 NSDictionary *props = [self propertiesForGid:enemy_tileGid]; NSString *value = [props valueForKey:@"type"]; if (value.length > 0) { [_enemyArray addObject:[MSEnemyInfo enemyWithType:[value intValue] point:towerLoc]]; } // 刪除精靈 SKSpriteNode *node = [_enemy tileAt:towerLoc]; [node removeFromParent]; } } } return; }
刪除地圖的精靈還有一種方式,上面這種方式只是刪除了地圖上的精靈,不會刪除精靈在圖層上的標記,因此正確的刪除應該是code
[_curtitleMap.enemy removeTileAtCoord:[_curtitleMap.enemy coordForPoint:point]];