---
title: "cocos2d-iphone之魔塔20层第六部分"
author: "mmdev"
date: "2013-02-24T04:19:00Z"
tags: []
slug: "cocos2d-iphone-20"
draft: false
lastmod: "2013-03-06T09:34:15Z"
id: 1823076
url: "https://mmdev.iteye.com/blog/1823076"
---

今天我们接着第五部分的开始讲，上一章我们让勇士和怪物可以战斗了，但是

我们会发现一个问题就是勇士的血量减少了，但是右面的生命值没有变化。

下面我们就开始添加这些标签的更新方法，让它们随着进度变化。

这些游戏数据的更新都是在Herohp这个类中进行的，需要添加的代码：

Herohp.h要添加代码

```
-(void) updateHeroHp;
-(void) updateHeroAttack;
-(void) updateHeroDefense;
-(void)updateCoin;
-(void)updateExperience;
-(void)updateGrade;
-(void) updateKey;
```

Herohp.m要添加代码

```
-(void)updateHeroHp
{
    [HpLable setString:[NSString stringWithFormat:@"生命 %d",self.hero.HP]];
}
-(void)updateHeroAttack
{
    [AttLable setString:[NSString stringWithFormat:@"攻击 %d",self.hero.Attack]];
}
-(void)updateHeroDefense
{
    [DefLable setString:[NSString stringWithFormat:@"防御 %d",self.hero.Defense]];
}
-(void)updateCoin
{
    [CoinLable setString:[NSString stringWithFormat:@"金币 %d",self.Coin]];
}
-(void)updateExperience
{
    [ExperienceLable setString:[NSString stringWithFormat:@"经验 %d",self.Experience]];
}
-(void)updateGrade
{
    [GradeLable setString:[NSString stringWithFormat:@"%d 级",self.Grade]];
}
-(void)updateKey1
{
    [Key1Lable setString:[NSString stringWithFormat:@"%d",self.YellowKey]];
    [Key2Lable setString:[NSString stringWithFormat:@"%d",self.BlueKey]];
    [Key3Lable setString:[NSString stringWithFormat:@"%d",self.RedKey]];
}
```

有了这些更新方法我们只要在数据变化的时候调用相应的更新方法就行了

首先我们Game01.m消除敌人（removeEnemy）方法中添加一行代码

```
[herohp updateHeroHp];
```

并在初始化方法里添加

herohp = \[HerohpsharedHP];

然后我们的勇士血量就会变化了。

接下来我们就要开始让我们的勇士真正的“吃”到地图上的道具了，首先介绍一下我们的道具

1、红血瓶（+生命 200） 2、蓝血瓶（+生命 500）

3、红宝石（+攻击 3） 4、蓝宝石（+防御 3）

5、红钥匙 （+1） 6、蓝钥匙（+1）

7、黄钥匙（+1） 8、大黄钥匙（各种钥匙+1）

9、金币（+500） 10、升级标

11、洞悉权杖 12、楼层飞行器

13、红榔头 14、十字架

15、生命魔瓶 16、宝剑、盾牌等

下面是代码部分我们根据图块属性来获取其相应是属性值，在if(item\_tileGid)中添加

```
        NSDictionary *props = [self.curtitleMap propertiesForGID:item_tileGid];
        NSString *value = [props valueForKey:@"HP"];
        NSString *value1 = [props valueForKey:@"Attack"];
        NSString *value2 = [props valueForKey:@"Defense"];
        NSString *value3 = [props valueForKey:@"key"];
        NSString *value4 = [props valueForKey:@"predict"];
        NSString *value5 = [props valueForKey:@"hoe"];
        NSString *value6 = [props valueForKey:@"double"];
        NSString *value7 = [props valueForKey:@"Promote"];
        NSString *value8 = [props valueForKey:@"grade"];
        NSString *value9 = [props valueForKey:@"coin"];
```

然后根据获取到的值改变勇士相应的数据

```
        if (value)
        {
            int hp = [value intValue];
            _hero.HP += hp;
            [herohp updateHeroHp];
        }
        if (value1) 
        {
            int attack = [value1 intValue];
            _hero.Attack +=attack;
            [herohp updateHeroAttack];
        }
        if (value2) 
        {
            int defense = [value2 intValue];
            _hero.Defense +=defense;
            [herohp updateHeroDefense];
        }
        if (value3) 
        {
            int key = [value3 intValue];
            switch (key) 
            {
                case 1:
                    herohp.YellowKey ++;
                    break;
                case 2:
                    herohp.BlueKey ++;
                    break;
                case 3:
                    herohp.RedKey ++;
                    break;
                case 4:
                    herohp.YellowKey ++;
                    herohp.BlueKey ++;
                    herohp.RedKey ++;
                    break;
                default:
                    break;
            }
            [herohp updateKey1];
        }
        if (value4) 
        {
        }
        if (value5) 
        {
        }
        if (value6) 
        {
            _hero.HP *= 2;
            [herohp updateHeroHp];
        }
        if (value7) 
        {
            _hero.HP *= 1.3;
            _hero.Attack *= 1.3;
            _hero.Defense *= 1.3;
            [herohp updateHeroHp];
            [herohp updateHeroAttack];
            [herohp updateHeroDefense];
        }
        if (value8) 
        {
            int grade =  [value8 intValue];
            _hero.HP += 1000*grade;
            _hero.Attack += 7*grade;
            _hero.Defense += 7*grade;
            herohp.Grade += grade;
            [herohp updateGrade];
            [herohp updateHeroHp];
            [herohp updateHeroAttack];
            [herohp updateHeroDefense];
        }
        if (value9)
        {
            int coin =  [value9 intValue];
            herohp.Coin += coin;
            [herohp updateCoin];
        }
```

这样我们的勇士就可以“吃”到各种道具了运行一下

游戏截图：

![](http://img.my.csdn.net/uploads/201302/24/1361677433_7136.png)

我们的勇士在打斗的时候是不能进行操作的所以我们要在creatFightScene方法中添加

\_hero.isFighting =YES;

并在打斗结束时赋值为NO![](http://img.my.csdn.net/uploads/201302/24/1361678549_3210.png)

下面我们要添加钥匙的使用和开门动画，在if(door\_tileGid)中添加

```
_hero.isFighting = YES;
        canmove = NO;
        NSDictionary *props = [self.curtitleMap propertiesForGID:door_tileGid];
        NSString *value = [props valueForKey:@"door"];
        if (value) 
        {
            int type = [value intValue];
            bool canoppen = NO;
            switch (type) 
            {
                case 0:
                    if (herohp.YellowKey > 0) 
                    {
                        herohp.YellowKey --;
                        canoppen = YES;
                    }
                    break;
                case 1:
                    if (herohp.BlueKey > 0) 
                    {
                        herohp.BlueKey --;
                        canoppen = YES;
                    }
                    break;
                case 2:
                    if (herohp.RedKey > 0) 
                    {
                        herohp.RedKey --;
                        canoppen = YES;
                    }
                    break;
                default:
                    break;
            }
            if (canoppen) 
            {
                [herohp updateKey1];
                CCTexture2D *heroTexture = [[CCTextureCache sharedTextureCache]addImage:@"door.png"];
                CCSpriteFrame *frame0,*frame1,*frame2,*frame3;
                //第二个参数表示显示区域的x,y,width,height,根据direction来确定显示的y坐标
                frame0 = [CCSpriteFrame frameWithTexture:heroTexture rect:CGRectMake(32*type, 32*0, 32, 32)];
                frame1 = [CCSpriteFrame frameWithTexture:heroTexture rect:CGRectMake(32*type, 32*1, 32, 32)];
                frame2 = [CCSpriteFrame frameWithTexture:heroTexture rect:CGRectMake(32*type, 32*2, 32, 32)];
                frame3 = [CCSpriteFrame frameWithTexture:heroTexture rect:CGRectMake(32*type, 32*3, 32, 32)];
                NSMutableArray *animFrames = [NSMutableArray array];
                [animFrames addObject:frame0];
                [animFrames addObject:frame1];
                [animFrames addObject:frame2];
                [animFrames addObject:frame3];
                //循环动画序列
                CCAnimation *animation = [CCAnimation animationWithFrames:animFrames delay:0.1f];
                CCAnimate *animate = [CCAnimate actionWithAnimation:animation];
                id action = [CCCallFunc actionWithTarget:self selector:@selector(removeDoor)];
                CCSequence *seq = [CCSequence actions: animate,action,nil];
                //
                [[self.curtitleMap.door tileAt:towerLoc] runAction:seq];
            }
            else
                _hero.isFighting = NO;
        }
```

在这里先判断了一下是否有相应的钥匙用于开门，如果有的话，相应的钥匙数量减少一把

并且播放开门动画，播放动画首先要加载动画纹理集，在获取每一帧的动画图片在把其加载

到动画序列中最后播放动画，在播放结束是调用消除门的方法

```
//消除已开门
-(void)removeDoor
{
    _hero.isFighting = NO;
    canmove = YES;
    [self.curtitleMap.door removeTileAt:towerLoc];
}
```

到这里我们的游戏也算有模有样了，但是我要说了是我们的游戏制作仅仅是刚开始，连着写了

三天，有点累了，今天就先到这里明天再继续我们的游戏制作

[上一篇连接](http://blog.csdn.net/fire_work/article/details/8605512) [下一篇连接](http://blog.csdn.net/fire_work/article/details/8607985)