《塔防类手游开发教程》 第十一节 根据关卡文件动态出怪

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

我们在玩塔防游戏时,每通过一个关卡,就会重新出现怪物。若是这样,有多少关卡,我们不是就得写多少关卡的代码,那么这样岂不是很繁琐。但是我们通过计划任务的停止,重载,动态的添加怪物,这样就节省了很多工作量。

    当通过第一个关卡,计划任务停止,移除所有的节点,在下一个关卡自动添加节点,这样就不会在原有节点上再添加,节点过多不好。其实就是地图没有关,把图层里面的东西全部都删掉,再重新加载一遍。

具体代码:

this->unscheduleAllSelectors();

this->removeAllChildren();

当进入下一关卡的时候,我们还需要当前关卡的怪物波数,当前是第几波,一个多少波,怪物的类型及HP的信息。

具体代码:

//当前关卡一共多少波

    this->levelAllNpc=levelInfo["npcgroup"].asValueVector();

    this->npcGroupCount=levelAllNpc.size();

    this->npcGroup_index=0;

    this->npcNumberCount=levelAllNpc.at(npcGroup_index).asValueVector().size();

    this->npcNumber_index=0;

    //添加提示 当前第几波

    auto  nowGroupLabel = Label::createWithBMFont("bitmapFontChinese.fnt"" ");

    nowGroupLabel->setPosition(Vec2(spritetool->getContentSize().width / 4+100, spritetool->getContentSize().height / 2));

    nowGroupLabel->setAnchorPoint(Point(0, 0.5f));

   

    nowGroupLabel->setString( std::to_string(npcGroup_index+1));

    nowGroupLabel->setTag(2003);

spritetool->addChild(nowGroupLabel);

 

    //取出当前波 当前个怪物的类型和hp

    int type=0;

    int hp=0;

    if (npcNumber_index<npcNumberCount) {

      type=levelAllNpc.at(this->npcGroup_index).asValueVector().at(npcNumber_index).asValueMap()["npcType"].asInt();

      hp=levelAllNpc.at(this->npcGroup_index).asValueVector().at(npcNumber_index).asValueMap()["npcHp"].asInt();

        npcNumber_index++;     

        Enemy * e1=Enemy::createEnemy(type,hp);

        this->addChild(e1);

        allEnemy.pushBack(e1);

    }else{

        npcNumber_index=0;

        npcGroup_index++;//下一波

        if (npcGroup_index>=levelAllNpc.size()) {

            //停止产生怪物

            this->unschedule(schedule_selector(GameScene::newEnemy));

            return;

        }

0 0
原创粉丝点击