加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2d-x-3.2塔防游戏开发3:动态的从配置文件中设置怪物的关卡

发布时间:2020-12-14 19:41:43 所属栏目:百科 来源:网络整理
导读:1.首先设置配置文件---在resources中新建property.list文件, 当前关的背景文件地图文件levelinfo--bgimg+mapfile 当前关的几波怪物npcgroup--每关有几波--123波,每波怪物有多少个怪物-设有123个,每个怪物都有各自的类型和血量 实现资源的读取和文件的保存


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

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读