您的位置:首页 > 移动开发 > Cocos引擎

Cocos2D:塔防游戏制作之旅(十六)

2015-11-17 10:10 281 查看
编译运行你的app,放置一些炮塔在你的地图上吧!你将看到炮塔在敌人移动如攻击范围时如何立即开始攻击,并且敌人的血条将随着攻击不断减少知道它们被人道毁灭!胜利即将来临了!



哦!Okay,这里只有少数细节还未实现你就可以得到一个完整特性的塔防游戏啦!音效应该是一个不错的尝试.并且尽管不可战胜和极端富裕很好,你的基地还是应该有能力持续抗打的能力 - 并且你需要限制玩家的金币供给.



闪耀着的炮塔:Gotta Polish It All!

开始实现显示玩家剩余的命数 - 以及当玩家失败时发生什么!

打开HelloWorldLayer.h并且添加如下3个实例变量:

int playerHp;
CCLabelBMFont *ui_hp_lbl;
BOOL gameEnded;


playerHP确定玩家有多少条命,CCLabelBMFont标签用来显示它们的数量.gameEnded在游戏结束时被设置!同样添加以下方法定义:

-(void)doGameOver;


现在打开HelloWorldLayer.m文件,完成以下修改:

// At the end of init, add the following lines of code:
// 7 - Player lives
playerHp = 5;
ui_hp_lbl = [CCLabelBMFont labelWithString:[NSString stringWithFormat:@"HP: %d",playerHp]
fntFile:@"font_red_14.fnt"];
[self addChild:ui_hp_lbl z:10];
[ui_hp_lbl setPosition:ccp(35,winSize.height-12)];

// Add the following methods
-(void)getHpDamage {
playerHp--;
[ui_hp_lbl setString:[NSString stringWithFormat:@"HP: %d",playerHp]];
if (playerHp <=0) {
[self doGameOver];
}
}

-(void)doGameOver {
if (!gameEnded) {
gameEnded = YES;
[[CCDirector sharedDirector]
replaceScene:[CCTransitionRotoZoom transitionWithDuration:1
scene:[HelloWorldLayer scene]]];
}
}


这里添加了一个方法去减少玩家的生命,更新标签的显示以及检查是否玩家耗完了所有的生命.如果是,则游戏结束.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: