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

来源:互联网 发布:中国电信大数据事业部 编辑:程序博客网 时间:2024/05/22 06:44

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

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

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));

0 0
原创粉丝点击