《塔防类手游开发教程》 第九节 怪物加入血槽以及金币数值

在塔防游戏中,每一关都会出现怪物。那么怪物有该拥有血槽,否则让炮塔一炮打死,游戏就没有什么难度了。说到炮塔,我们要建炮塔,就得有经济,也就是金币,没有金币就无法建塔,只能眼睁睁看着怪物跑过去。不这样设置的话,炮塔无限建,怪物秒死,这样的游戏应该没人去玩。

下面我们就来学习下具体的方法:

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值