《塔防类手游开发教程》 第九节 怪物加入血槽以及金币数值
2015-05-06 22:15
183 查看
在塔防游戏中,每一关都会出现怪物。那么怪物有该拥有血槽,否则让炮塔一炮打死,游戏就没有什么难度了。说到炮塔,我们要建炮塔,就得有经济,也就是金币,没有金币就无法建塔,只能眼睁睁看着怪物跑过去。不这样设置的话,炮塔无限建,怪物秒死,这样的游戏应该没人去玩。
下面我们就来学习下具体的方法:
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));
相关文章推荐
- 《塔防类手游开发教程》 第九节 怪物加入血槽以及金币数值
- openfire Android学习(三)----会议室创建、加入以及查询会议室中所有成员等
- 零基础学python-2.15 回到我们的游戏 加入for以及列表
- 第九节 java数组的介绍,数组定义,数组排序,Arrays类静态方法使用,以及源码解析
- 免安装版tomcat安装配置以及将tomcat加入系统服务中
- C语言中的正负数以及数值溢出
- 第七课:数值以及函数的扩展和修复
- maven打包加入依赖包以及加入本地依赖包的方法
- Lucene的数值索引以及范围查询
- android sdk 编译--如何将源代码加入android.jar,以及make原理
- 零基础学python-2.15 回到我们的游戏 加入for以及列表
- 应用开机自启动,以及加入灭屏,亮屏功能
- 实现app上对csdn的文章列表上拉刷新下拉加载以及加入缓存文章列表的功能 (制作csdn app 四)
- C++基础教程(一)——Hello World以及计算两个数值的和
- 数值格式误差以及收敛精度估计方法
- IOS源码封装成.bundle和.a文件,以及加入xib的具体方法,翻遍网络,仅此一家完美翻译!! IOS7!!(3) 完美结局
- android 加入广告页的思路以及实现。
- 非数值数据的表示以及十进制数的二进制编码(BCD)表示
- 数值分析Matlab二维正态(高斯)分布以及协方差矩阵
- 判断两个数值中最大的一个以及不用中间变量交换两个数