《塔防类手游开发教程》 第九节 怪物加入血槽以及金币数值
2016-06-17 15:51
288 查看
在塔防游戏中,每一关都会出现怪物。那么怪物有该拥有血槽,否则让炮塔一炮打死,游戏就没有什么难度了。说到炮塔,我们要建炮塔,就得有经济,也就是金币,没有金币就无法建塔,只能眼睁睁看着怪物跑过去。不这样设置的话,炮塔无限建,怪物秒死,这样的游戏应该没人去玩。
下面我们就来学习下具体的方法:
1. 在敌人类中添加血槽:
//添加血槽
auto hp=LoadingBar::create("sliderProgress2.png");
hp->setTag(1000);
hp->setPercent(100);
newe->addChild(hp,1);
hp->setPositionY(60);
2. 定义一个void changeHP(),当血改变时调用这个函数
void Enemy::changeHp(){
LoadingBar * hp=(LoadingBar * )this->getChildByTag(1000);
int progress=(this->hp/(float)fullHp*100);
hp->setPercent(progress);
}
就使用这么简单几行代码,就实现了对怪物加入血槽。接下来我们再看看如何创建金币数量。
1. 首先初始化每一个关的金币数量
//初始化钱
this->money=levelInfo["levelinfo"].asValueMap()["money"].asInt();
sp->setPosition(Vec2(Director::getInstance()->getWinSize().width/2,
Director::getInstance()->getWinSize().height/2));
2. 根据每个塔多少钱,相应的减掉金币,如果钱不够给出提示不能建塔
void GameScene::selectTD(Ref *obj){
auto item=(MenuItemSprite *)obj;
switch (item->getTag()) {
case 10://塔1
{
TD * newTd=TD::createTD(1,8-nowRow,nowCol);
this->addChild(newTd);
if(this->money>=newTd->price)
{
mapinfo[nowRow][nowCol]=1;//标记这个位置已经有塔
this->money-=newTd->price;
auto moneyLabel=(Label *)this->getChildByTag(2000)->getChildByTag(2002);
moneyLabel->setString(StringUtils::format("%d",money));
}
else{
this->removeChild(newTd);
auto tips = Sprite::createWithSpriteFrameName(
"nomoney_mark.png");
tips->setAnchorPoint(Vec2(0,0));
// CCLOG("tiled width%f tips width%f",map->getTileSize().width,tips->getContentSize().width);
tips->setPosition(nowCol*71,(8-nowRow)*71);
this->addChild(tips);
tips->runAction(Sequence::create(DelayTime::create(0.8f),
CallFunc::create(CC_CALLBACK_0(Sprite::removeFromParent, tips)),
NULL));
}
}
3. 既然有钱才能建塔,我们不能光花钱不能挣钱,不然只能建一两个塔,怎么能打死一堆怪物呢。
if(e->hp<=0)
{ //赚钱
this->money+=100;
auto moneyLabel=(Label *)this->getChildByTag(2000)->getChildByTag(2002);
moneyLabel->setString(StringUtils::format("%d",money));
下面我们就来学习下具体的方法:
1. 在敌人类中添加血槽:
//添加血槽
auto hp=LoadingBar::create("sliderProgress2.png");
hp->setTag(1000);
hp->setPercent(100);
newe->addChild(hp,1);
hp->setPositionY(60);
2. 定义一个void changeHP(),当血改变时调用这个函数
void Enemy::changeHp(){
LoadingBar * hp=(LoadingBar * )this->getChildByTag(1000);
int progress=(this->hp/(float)fullHp*100);
hp->setPercent(progress);
}
就使用这么简单几行代码,就实现了对怪物加入血槽。接下来我们再看看如何创建金币数量。
1. 首先初始化每一个关的金币数量
//初始化钱
this->money=levelInfo["levelinfo"].asValueMap()["money"].asInt();
sp->setPosition(Vec2(Director::getInstance()->getWinSize().width/2,
Director::getInstance()->getWinSize().height/2));
2. 根据每个塔多少钱,相应的减掉金币,如果钱不够给出提示不能建塔
void GameScene::selectTD(Ref *obj){
auto item=(MenuItemSprite *)obj;
switch (item->getTag()) {
case 10://塔1
{
TD * newTd=TD::createTD(1,8-nowRow,nowCol);
this->addChild(newTd);
if(this->money>=newTd->price)
{
mapinfo[nowRow][nowCol]=1;//标记这个位置已经有塔
this->money-=newTd->price;
auto moneyLabel=(Label *)this->getChildByTag(2000)->getChildByTag(2002);
moneyLabel->setString(StringUtils::format("%d",money));
}
else{
this->removeChild(newTd);
auto tips = Sprite::createWithSpriteFrameName(
"nomoney_mark.png");
tips->setAnchorPoint(Vec2(0,0));
// CCLOG("tiled width%f tips width%f",map->getTileSize().width,tips->getContentSize().width);
tips->setPosition(nowCol*71,(8-nowRow)*71);
this->addChild(tips);
tips->runAction(Sequence::create(DelayTime::create(0.8f),
CallFunc::create(CC_CALLBACK_0(Sprite::removeFromParent, tips)),
NULL));
}
}
3. 既然有钱才能建塔,我们不能光花钱不能挣钱,不然只能建一两个塔,怎么能打死一堆怪物呢。
if(e->hp<=0)
{ //赚钱
this->money+=100;
auto moneyLabel=(Label *)this->getChildByTag(2000)->getChildByTag(2002);
moneyLabel->setString(StringUtils::format("%d",money));
相关文章推荐
- CentOS6.5下SNMP安装
- python实现文本文件转二进制文件(二进制序列化)
- Linux(Centos、Debian)之安装Java JDK及注意事项(转)
- 补、C++第九次作业
- 方法
- Java对象锁和类锁全面解析(多线程synchronized关键字)
- Java对象锁和类锁全面解析(多线程synchronized关键字)
- Java对象锁和类锁全面解析(多线程synchronized关键字)
- Java-异常【占坑中】
- 二维数组作为形参进行传递
- 【剑指offer】(扩展)Excel字母表示列号
- 测试理论知识
- spark-1.6.1 伪分布式 & 全分布式安装
- 《塔防类手游开发教程》 第十二节 过关后关卡切换
- html5_ajax无刷新文件上传
- tomat报错OutOfMemoryError:PermGen space解决方法 不改catalina.sh
- DFA确定性有限状态机过滤敏感词
- RPC 框架
- .so兼容32位和64位
- Deep Learning(深度学习)之(三)Deep Learning的常用模型或者方法