《塔防类手游开发教程》 第十节 创建关卡配置文件动态读取关卡信息

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

每个关卡都有塔的信息,我们这个塔防可能有100关,怎么来切换怎么来加载,每个关卡都有不同的地图文件,不同的地图背景,加载的时候都有一个背景层;每个关卡都有不同的金钱,不同的怪物波数,不同的怪物hp,type。在游戏场景中,当level_number=1,加载第一个关卡,如果所有怪物都被打死,则加载下一个关卡。这节课我们就来学习下关卡的切换

首先在项目中新建一个.plist配置文件,作为第一关的地图信息,在里面创建新的关卡子节点。

子节点leveInfo ,type:Dictionary,为数据字典,在里面再添加bmimg,type:string ;mapfile,type:string;

子节点npcgroup,type:Array(数组/集合),添加Item0,type:Array(为第几波),再在Item0下添加Item 0,type:Dictionary(为第几个怪物),再该Item0下添加怪物类型,npcType,npcHP。以此类推,可以创建N波怪物,N种怪物类型。

我们直接复制这个配置文件,再粘贴,我们就可以编辑第二关的地图信息,若是有100关,那么就有100个配置文件。使用这个配置文件的好处,当以后游戏需要更新的时候,我们从网上下载该配置文件修改即可,方便省时。

编辑好配置文件之后,还需要通过代码调用配置文件。

具体代码:

在GameScene.h中

int npcGroupCount;//当前关共有多少波怪物

    int npcNumberCount;//当前波共有多少个怪物

    int npcGroup_index;//当前第几波

void initLevel();//初始化当前关卡

在GameScene.cpp中的init()方法中调用“初始化当前关卡”

this->nowLevel=1;//从第一关开始

this->initLevel();

 

在GameScene.cpp中实现initLevel()方法

void GameScene::initLevel()//初始化当前关卡

{

    this->unscheduleAllSelectors();

    this->removeAllChildren();

    this->allPoint.clear();

    playerHp=100;

    //----------

    ValueMap levelInfo=FileUtils::getInstance()->getValueMapFromFile(

                                    StringUtils::format("gameLevel00%d.plist",nowLevel));

    //获取背景信息

    std::string bg= levelInfo["levelinfo"].asValueMap()["bgimg"].asString();

    //加载背景

    auto sp=Sprite::create(bg);

    this->addChild(sp);

    //加载金币

    //初始化钱

    this->money=levelInfo["levelinfo"].asValueMap()["money"].asInt();

    

    sp->setPosition(Vec2(Director::getInstance()->getWinSize().width/2,

                         Director::getInstance()->getWinSize().height/2));

    //加载地图

    std::string mapf=levelInfo["levelinfo"].asValueMap()["mapfile"].asString();

    auto map=TMXTiledMap::create(mapf);

    this->addChild(map);

    map->setTag(888);

    //加载所有点

    initAllPoint(map);

    //产生敌人

    this->schedule(schedule_selector(GameScene::newEnemy),3);

    this->enemyCount=20;

    auto spritetool = Sprite::createWithSpriteFrameName("toolbg.png");

    spritetool->setAnchorPoint(Point(0.5f, 1));

    spritetool->setPosition (Vec2(Director::getInstance()->getWinSize().width / 2,

                                  Director::getInstance()->getWinSize().height));

    this->addChild(spritetool);

    spritetool->setTag(2000);

    //

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

    moneyLabel->setPosition(Vec2(spritetool->getContentSize().width / 8, spritetool->getContentSize().height / 2));

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

    auto moneyText = std::to_string(money);

    moneyLabel->setString(moneyText);

    moneyLabel->setTag(2002);

spritetool->addChild(moneyLabel);

this->scheduleUpdate();

}


0 0
原创粉丝点击