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


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

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

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

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


2.动态的加载关卡:

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

int money;

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

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

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

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

int npcNumber_index;@H_502_29@//@H_502_29@当前第几个

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

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

@H_502_29@

GameScene.cpp

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

void GameScene::initLevel(){

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

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

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

@H_502_29@//@H_502_29@从当前的关卡中

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

ValueMapleveInfo=FileUtils::getInstance()->getValueMapFromFile(StringUtils::format("gameLevel00%d.plist",NowLevel));

@H_502_29@//@H_502_29@获取背景信息

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

@H_502_29@//@H_502_29@动态的@H_502_29@加载背景

auto bg=Sprite::create(bg1);

this->addChild(bg);

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

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

@H_502_29@//@H_502_29@加载金币@H_502_29@—@H_502_29@动态的读取

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

@H_502_29@//@H_502_29@加载地图@H_502_29@—@H_502_29@动态读取

std::stringmapf=leveInfo["levelinfo"].asValueMap()["mapfile"].asstring();

auto map=TMXTiledMap::create(mapf);

this->addChild(map);

map->setTag(888);

@H_502_29@//@H_502_29@加载所有的点

initAllPoint(map);

@H_502_29@//@H_502_29@定时的移动,和产生怪物

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

@H_502_29@//@H_502_29@碰撞

this->scheduleUpdate();

@H_502_29@//@H_502_29@初始化钱数

auto spritetool = Sprite::createWithSpriteFrameName("toolbg.png");@H_502_29@//@H_502_29@建显示钱工具

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

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

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

this->addChild(spritetool);

spritetool->setTag(2000);

@H_502_29@//

auto moneyLabel = Label::createWithBMFont("bitmapFontChinese.fnt"," ");@H_502_29@//@H_502_29@显示数量标签

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();@H_502_29@//npcgroup@H_502_29@这是一个@H_502_29@valuevector@H_502_29@。@H_502_29@ //@H_502_29@当前关卡一共多少波

this->npcGroupCount=levelAllNpc.size();@H_502_29@//@H_502_29@这是当前关总共的怪物数量

this->npcGroup_index=0;@H_502_29@//@H_502_29@这是第几波

this->npcNumberCount=levelAllNpc.at(npcGroup_index).asValueVector().size();@H_502_29@//@H_502_29@这是当前波总共的怪物数量

this->npcNumber_index=0;@H_502_29@//@H_502_29@当前第几个

@H_502_29@//@H_502_29@添加第几波的提示

auto NowGroupLabel = Label::createWithBMFont("bitmapFontChinese.fnt"," ");@H_502_29@//@H_502_29@显示数量标签

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

@H_502_29@//@H_502_29@添加一共有多少波

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

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

@H_502_29@//@H_502_29@取出当前波@H_502_29@当前怪唔得类型和@H_502_29@hp

int type=0;

int hp=0;

if(npcNumber_index<npcNumberCount){@H_502_29@//@H_502_29@如果出来的怪物》这是当前波总共的怪物数量@H_502_29@-----@H_502_29@也就是如果这一波都产生完了就让他产生下一波

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();@H_502_29@//npcHp@H_502_29@是@H_502_29@plist@H_502_29@中的

npcNumber_index++;@H_502_29@//

auto e1=Enemy::createEnemy(type,hp);//产生怪物

this->addChild(e1);

allEnemy.pushBack(e1);

}else{

npcNumber_index=0;

npcGroup_index++;@H_502_29@//@H_502_29@下一波怪物产生

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

@H_502_29@//@H_502_29@停止产生怪物

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

return;

}

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

@H_502_29@//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.这时,有几波怪物已经都产生完了,接下来我们该做的操作就是在碰撞检测中检测是否过关,如果过关那么我们就让它产生一个过关的动画,然后自动跳转到下一关:

@H_502_29@

@H_502_29@

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

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

this->unscheduleUpdate();

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

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

if (NowLevel>4){

this->unscheduleAllSelectors();

@H_502_29@//@H_502_29@返回主菜单

auto scene=MenuScene::createScene();

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

}

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

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

}

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...