cocos2d-x-3.2塔防游戏开发3:动态的从配置文件中设置怪物的关卡,总波数,初始化钱数

cocos2d-x-3.2塔防游戏开发3:动态的从配置文件中设置怪物的关卡,总波数,初始化钱数,第1张

概述 1.首先设置配置文件---在resources中新建property.list文件,      当前关的背景文件地图文件levelinfo--bgimg+mapfile      当前关的几波怪物 npcgroup--每关有几波--123波,每波怪物有多少个怪物-设有123个,每个怪物都有各自的类型和血量      实现资源的读取和文件的保存,这样我就可以定义无数的关卡:如图     2.动态的


1.首先设置配置文件---在resources中新建property.List文件,

当前关的背景文件地图文件levelinfo--bgimg+mapfile

当前关的几波怪物npcgroup--每关有几波--123波,每波怪物有多少个怪物-设有123个,每个怪物都有各自的类型和血量

实现资源的读取和文件的保存,这样我就可以定义无数的关卡:如图


2.动态的加载关卡:

接下来就是定义代码文件了:.h文件

int money;

int NowLevel;//当前管卡的编号

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

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

int npcGroup_index;//当前第几波怪物

int npcNumber_index;//当前第几个

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

ValueVector levelAllNpc;//当前关卡的所有怪唔得定义

GameScene.cpp

接下来我们就要初始化当前关卡了,把init()中的加载放到这里:

voID GameScene::initLevel(){

this->unscheduleAllSelectors();//停掉计划任务

this->removeAllChildren();//移除关卡的所有内容--------为了切换到下一关时不再加载第二次

this->allPoint.clear();//清空所有点得数据从新加载--为了到下一关的时候从新加载路径

//从当前的关卡中

// cocos中有一个很好用的类叫fileUtils::文件夹读取工具,读取这个文件,写活%d

ValueMapleveInfo=fileUtils::getInstance()->getValueMapFromfile(StringUtils::format("gameLevel00%d.pList",NowLevel));

//获取背景信息

std::stringbg1=leveInfo["levelinfo"].asValueMap()["bgimg"].asstring();

//动态的加载背景

auto bg=Sprite::create(bg1);

this->addChild(bg);

bg->setposition(Director::getInstance()->getWinSize().wIDth/2,

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

//加载金币动态的读取

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

//加载地图动态读取

std::stringmapf=leveInfo["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->scheduleUpdate();

//初始化钱数

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->levelAllNpc=leveInfo["npcgroup"].asValueVector();//npcgroup这是一个valuevector //当前关卡一共多少波

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

//添加一共有多少波

auto GroupLabel = Label::createWithBMFont("bitmapFontChinese.fnt"," ");//显示钱数量的标签

GroupLabel->setposition(Vec2(spritetool->getContentSize().wIDth /3+150,spritetool->getContentSize().height /2));

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

GroupLabel->setString(std::to_string(npcGroupCount));

GroupLabel->setTag(2004);

spritetool->addChild(GroupLabel);

3.这样初始化怪物就好了,接下来就是在产生怪物的计划任务newEnemy中取出当前波,当前怪物的类型和hp,产生怪物。

voID GameScene::newEnemy(float t){

//取出当前波当前怪唔得类型和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();//npcHppList中的

npcNumber_index++;//

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

}

//处理:让第几波的那个数字更新

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

auto NowGroupLabel=(Label*)this->getChildByTag(2000)->getChildByTag(2003);

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

}

}

4.这时,有几波怪物已经都产生完了,接下来我们该做的 *** 作就是在碰撞检测中检测是否过关,如果过关那么我们就让它产生一个过关的动画,然后自动的跳转到下一关:

//检测是否过关---第四波都出来了,》0allenemy中的怪物没有都移除了--也就是说都被打死了

if (this->npcGroup_index>=this->npcGroupCount && allEnemy.size()==0){

this->unscheduleUpdate();

this->NowLevel++;//关卡++

//全部的关卡完成,自动返回主菜单

if (NowLevel>4){

this->unscheduleAllSelectors();

//返回主菜单

auto scene=MenuScene::createScene();

Director::getInstance()->replaceScene(scene);

}

//就让它出现一个动画--过关

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

tips->setString("Win Game");

tips->setposition(Vec2(Director::getInstance()->getWinSize().wIDth/2,-100));

this->addChild(tips);

tips->runAction(Sequence::create(Moveto::create(0.8f,Vec2(Director::getInstance()->getWinSize().wIDth/2,Director::getInstance()->getWinSize().height/2)),

CallFunc::create(CC_CALLBACK_0(Node::removeFromParent,tips)),

CallFunc::create(CC_CALLBACK_0(GameScene::initLevel,this)),

NulL));

}

总结

以上是内存溢出为你收集整理的cocos2d-x-3.2塔防游戏开发3:动态的从配置文件中设置怪物的关卡,总波数,初始化钱数全部内容,希望文章能够帮你解决cocos2d-x-3.2塔防游戏开发3:动态的从配置文件中设置怪物的关卡,总波数,初始化钱数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1018926.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存