当前位置:编程学习 > wap >>

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

今天我们接着第五部分的开始讲,上一章我们让勇士和怪物可以战斗了,但是
我们会发现一个问题就是勇士的血量减少了,但是右面的生命值没有变化。
下面我们就开始添加这些标签的更新方法,让它们随着进度变化。
这些游戏数据的更新都是在Herohp这个类中进行的,需要添加的代码:
Herohp.h要添加代码
[html]  
-(void) updateHeroHp;  
-(void) updateHeroAttack;  
-(void) updateHeroDefense;  
-(void)updateCoin;  
-(void)updateExperience;  
-(void)updateGrade;  
-(void) updateKey;  
 
Herohp.m要添加代码
[html] 
-(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)方法中添加一行代码
[html] 
[herohp updateHeroHp];  
 
并在初始化方法里添加
herohp = [Herohp sharedHP];
然后我们的勇士血量就会变化了。
接下来我们就要开始让我们的勇士真正的“吃”到地图上的道具了,首先介绍一下我们的道具
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)中添加
 
[html]  
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"];  
 
然后根据获取到的值改变勇士相应的数据
 
[html]  
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 up
补充:移动开发 , 其他 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,