动态数据类CDynamicData中:
CC_SYNTHESIZE(int, m_nLevelId, LevelId); //在关卡选择的时候保存这个数据
在LevelSel中:
void CLevelSel::onBtnStart(Ref* pRender) //关卡选择页面开始游戏的回调函数
{
//获取当前所选的m_pPageView的索引
int nIndex = m_pPageView->getCurPageIndex();
//由于引擎不操作时nIndex默认为-1,用三目运算符修改一下
nIndex = (nIndex >= 0) ? nIndex: 0;
//设置关卡Id,即要跳转的关卡
int nLevelId = nIndex + 1001;
CDynamicData::getInstance()->setLevelId(nLevelId); //修改关卡Id
//切换到游戏场景
Director::getInstance()->replaceScene(CGameScene::getInstance());
}
在background中:
//实现效果:不同关卡背景图不同
//接收关卡Id(在动态数据CDynamicData里)
int nLevelId = CDynamicData::getInstance()->getLevelId();
//通过不同的关卡Id 获取关卡数据
CLevelDt* pLevelDt = CConfigMgr::getInstance()->getData("LevelDtMgr")->getDataByID<CLevelDt*>(nLevelId);
for (int i = 0; i < pLevelDt->vecBgImage.size(); i++)
{
//Sprite* pBg = Sprite::create(arrPng[i]); //法1
//sprintf_s(szName, "Image/Map%d.png", i + 1); //法2
//Sprite* pBg = Sprite::create(szName);
//根据关卡配置表的背景来创建背景
string strName = pLevelDt->vecBgImage[i];
Sprite* pBg = Sprite::create(strName);
this->addChild(pBg); //记得加入当前场景
pBg->setAnchorPoint(Vec2(0.5, 0));
pBg->setPosition(origin.x + visibleSize.width / 2, fy);
fy += pBg->getContentSize().height;
//别忘了压入容器
m_vecBg.push_back(pBg);
}
在EnemyMgr中:
//怪物根据不同关卡按波出现
int nLevelId = CDynamicData::getInstance()->getLevelId();
pLevelDt = CConfigMgr::getInstance()->getData("LevelDtMgr")->getDataByID<CLevelDt*>(nLevelId);
//通过关卡拿到怪物波(定义一个索引)
m_pCurWave = pLevelDt->vecWaves[m_nWaveIndex];