接下来我们就要添加勇士的移动检测了,再添加移动检测之前我们要把我们自己制作的地图进行解析
首先我们基于cocos2d建一个类,类名为:TitledMap 这个类继承CCTMXTiledMap,建成如下:
下面我们开始添加TitledMap.h中的代码
之前我说过我们的地图由多个图层组成,所以在这个类中我们每一个图层都相应的设置一个属性
还有上一章说过的地图上还有两个勇士的问题,其实它们代表两个坐标点分别是上楼和下楼的位置
所以我们再添加两个属性,代码如下:
[html] www.zzzyk.com
@property (nonatomic,retain) CCTMXLayer *wall;
@property (nonatomic,retain) CCTMXLayer *road;
@property (nonatomic,retain) CCTMXLayer *enemy;
@property (nonatomic,retain) CCTMXLayer *item;
@property (nonatomic,retain) CCTMXLayer *upfloor;
@property (nonatomic,retain) CCTMXLayer *downfloor;
@property (nonatomic,retain) CCTMXLayer *door;
@property (nonatomic,retain) CCTMXLayer *other;
@property (nonatomic,retain) CCTMXLayer *npc;
@property (nonatomic,retain) CCTMXLayer *heroPoint;
@property (nonatomic,assign) CGPoint up;
@property (nonatomic,assign) CGPoint down;
接着我们再添加一个类方法+(TitledMap*)initWithAnalytic:(int)tileMapName;
这个类方法主要是为了实现在其他文件中加载地图,同时这个类方法调用初始化方法
[html]
+(TitledMap*)initWithAnalytic:(int)tileMapName
{
return [[[self alloc] initWithAnalytic:tileMapName] autorelease];
}
-(id)initWithAnalytic:(int)tileMapName
{
NSString *string = [NSString stringWithFormat:@"%d.tmx",tileMapName];
self = [super initWithTMXFile:string];
if (self)
{
self.road = [self layerNamed:@"road"];
self.upfloor = [self layerNamed:@"upfloor"];
self.downfloor = [self layerNamed:@"downfloor"];
self.item = [self layerNamed:@"item"];
self.enemy = [self layerNamed:@"enemy"];
self.door = [self layerNamed:@"door"];
self.npc = [self layerNamed:@"npc"];
self.other = [self layerNamed:@"other"];
self.heroPoint = [self layerNamed:@"heroPoint"];
}
return self;
}
接下来我们要把地图上的那“两个勇士”去掉
[html]
-(void)titledMapAnalytic
{
for (int x = 0; x <= 10; x++)
{
for (int y = 0; y <= 10; y++)
{
CGPoint towerLoc = CGPointMake(x, y);
int heroPoint_tileGid = [self.heroPoint tileGIDAt:towerLoc];
if (heroPoint_tileGid)
{
NSDictionary *props = [self propertiesForGID:heroPoint_tileGid];
NSString *value = [props valueForKey:@"point"];
int type = [value intValue];
if (type == 1)
{
self.up = towerLoc;
}
else
self.down = towerLoc;
[self.heroPoint removeTileAt:towerLoc];
}
}
}
return;
}
到这里我们的地图解析只是进行了一部分,我们还要再添加一个简单而重要的类,它就是
游戏数据类GameModel
下面我们开始添加这个类里面的代码,代码不多我就不过多解释了,大家看不明白的可以提问
GameModel.h文件代码
[html]
//地图集合1
@property (nonatomic,retain) NSMutableArray *titleMapArray1;
//地图集合2
@property (nonatomic,retain) NSMutableArray *titleMapArray2;
//地图集合3
@property (nonatomic,retain) NSMutableArray *titleMapArray3;
+(GameModel*)getGameModel;
//加载地图
-(void)initWithMap;
GameModel.m文件代码
[html]
+(TitledMap*)initWithAnalytic:(int)tileMapName
{
return [[[self alloc] initWithAnalytic:tileMapName] autorelease];
}
-(id)initWithAnalytic:(int)tileMapName
{
NSString *string = [NSString stringWithFormat:@"%d.tmx",tileMapName];
self = [super initWithTMXFile:string];
if (self)
{
self.road = [self layerNamed:@"road"];
self.upfloor = [self layerNamed:@"upfloor"];
self.downfloor = [self layerNamed:@"downfloor"];
self.item = [self layerNamed:@"item"];
self.enemy = [self layerNamed:@"enemy"];
self.door = [self layerNamed:@"door"];
self.npc = [self layerNamed:@"npc"];
self.other = [self layerNamed:@"other"];
self.heroPoint = [self layerNamed:@"heroPoint"];
}
[self titledMapAnalytic];
return self;
}
-(void)titledMapAnalytic
{
for (int x = 0; x <= 10; x++)
{
for (int y = 0; y <= 10; y++)
{
CGPoint towerLoc = CGPointMake(x, y);