用过cocos2d都知道Tiled编辑的2d方块地图tmx,在cocos2d中有自己能解析的库,但是SpriteKit没有,怎么能做拼组地图了。
大神写了一个:https://github.com/slycrel/JSTileMap
定义地图类:
#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就是设计地图时的图层
-(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;
}
删除地图的精灵还有一种方式,上面这种方式只是删除了地图上的精灵,不会删除精灵在图层上的标记,所以正确的删除应该是
[_curtitleMap.enemy removeTileAtCoord:[_curtitleMap.enemy coordForPoint:point]];