`
yidongkaifa
  • 浏览: 4062941 次
文章分类
社区版块
存档分类
最新评论

cocos2d-iphone之魔塔20层第四部分

 
阅读更多

接下来我们就要添加勇士的移动检测了,再添加移动检测之前我们要把我们自己制作的地图进行解析

首先我们基于cocos2d建一个类,类名为:TitledMap 这个类继承CCTMXTiledMap,建成如下:


下面我们开始添加TitledMap.h中的代码

之前我说过我们的地图由多个图层组成,所以在这个类中我们每一个图层都相应的设置一个属性

还有上一章说过的地图上还有两个勇士的问题,其实它们代表两个坐标点分别是上楼和下楼的位置

所以我们再添加两个属性,代码如下:

@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;

这个类方法主要是为了实现在其他文件中加载地图,同时这个类方法调用初始化方法

+(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;
}

接下来我们要把地图上的那“两个勇士”去掉

-(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文件代码

//地图集合1
@property (nonatomic,retain) NSMutableArray *titleMapArray1;
//地图集合2
@property (nonatomic,retain) NSMutableArray *titleMapArray2;
//地图集合3
@property (nonatomic,retain) NSMutableArray *titleMapArray3;

+(GameModel*)getGameModel;
//加载地图
-(void)initWithMap;

GameModel.m文件代码

+(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);
            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;
}

下面我们就要在加载地图这个方法中添加如下代码

for (int i = 0; i < 22; i++) 
    {
        TitledMap *titledMap = [TitledMap initWithAnalytic:i];
        [self.titleMapArray1 addObject:titledMap];
    }

这些代码添加完我们的GameModel类就基本完成了。下面我们就要把刚才添加的两个类用在Game01中了

//加载游戏地图

curTiledMap= [CCTMXTiledMaptiledMapWithTMXFile:@"1.tmx"];

curTiledMap.scale=_scale;

curTiledMap.position=ccp(LEFTMIN,DOWNMIN);

[selfaddChild:curTiledMap];

修改成

//初始化数据
        self.model = [GameModel getGameModel];
        [model initWithMap];
        seltitleMapArray = self.model.titleMapArray1;
        //加载地图
        self.curtitleMap = [self.model.titleMapArray1 objectAtIndex:1];
        self.curtitleMap.scale = _scale;
        self.curtitleMap.position = ccp(LEFTMIN, DOWNMIN);
        [self addChild:self.curtitleMap];

修改过之后我们就要开始添加碰撞检测了

//检测是否有障碍物
-(void)canMoveTo:(CGPoint) pos
{
    towerLoc = [self tileCoordForPosition: pos];
    int road_tileGid = [self.curtitleMap.road tileGIDAt:towerLoc];
    int enemy_tileGid = [self.curtitleMap.enemy tileGIDAt:towerLoc];
    int item_tileGid = [self.curtitleMap.item tileGIDAt:towerLoc];
    int door_tileGid = [self.curtitleMap.door tileGIDAt:towerLoc];
    int npc_tileGid = [self.curtitleMap.npc tileGIDAt:towerLoc];
    int downfloor_tileGid = [self.curtitleMap.downfloor tileGIDAt:towerLoc];
    int upfloor_tileGid = [self.curtitleMap.upfloor tileGIDAt:towerLoc];
    int other_tileGid = [self.curtitleMap.other tileGIDAt:towerLoc];
    
    if (enemy_tileGid) 
    {
        canmove = NO;
    }
    if (item_tileGid) 
    {
        [self.curtitleMap.item removeTileAt:towerLoc];
    }
    if (door_tileGid) 
    {
        //canmove = NO;
        [self.curtitleMap.door removeTileAt:towerLoc];
    }
    if (other_tileGid) 
    {
        canmove = NO;
    }
    if (upfloor_tileGid) 
    {
        canmove = NO;
    }
    if (downfloor_tileGid) 
    {
        canmove = NO;
    }
    if (npc_tileGid) 
    {
        canmove = NO;
    }
    if (road_tileGid) 
    {
        if (canmove) 
        {
            [self setPlayerPosition:pos];
        }
    }
}

towerLoc = [self tileCoordForPosition:pos] 是把当前移动坐标转换成地图坐标

int road_tileGid = [self.curtitleMap.road tileGIDAt:towerLoc] 获取当前坐标

在相应的图层中是否有图块存在。

方法有了,我们就得调用它,把updateMove方法中的if循环中的内容换成

//判断是否有物品或怪物
[self canMoveTo:playerPoint];

然后运行你会发现勇士不能穿过墙和怪物了,但是还不能和怪物打斗,接下来我们就要在if循环里添加

相应的响应事件了




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics