重点内容 做游戏时候经常用到关卡选择,写了个关卡选择界面逻辑类,关卡解锁,关卡类型显示,打过的关卡星级评定,一共六大关,每个大关10个小关,ui使用cocostudio编辑,代码如下:
LevelSel.h文件
#ifndef _LEVELSEL_H
define _LEVELSEL_H
include “cocos2d.h”
include “cocos-ext.h”
include “global.h”
using namespace cocos2d;
using namespace cocos2d::extension;
class LevelSel : public CCLayer
{
public:
CREATE_FUNC(LevelSel);
bool init();
void selevent(CCObject *pSender,TouchEventType type);
void pageViewEvent(CCObject *pSender,PageViewEventType type);
void update(float delta);
void onEnter();
UIPageView* pageView;
Layout* layout_sel;
void refreshsel(int offset);
void aaaaa();
//////////////////////////////////////////////////////////////////////////
int stars[60];
int presel,cursel;
int curlevel;
};
endif
LevelSel.cpp文件
include “LevelSel.h”
include “Modules/Archive.h”
include “Scene/MapPrepareScene.h”
include “Common/EnterStageDataExchange.h”
include “PropLayer.h”
void LevelSel::refreshsel(int curoffset) { UIButton* Upgrade_bt; char str[128]; for(int i=1000;i<=10000;i+=1000) { Upgrade_bt = (UIButton *)(layout_sel->getChildByTag(i)); Upgrade_bt->addTouchEventListener(this,toucheventselector(LevelSel::selevent)); int idx=i/1000-1; UIImageView* modeuw=(UIImageView*)layout_sel->getChildByTag(i+2); if(stars[idx+curoffset]>1) { //显示数字 Upgrade_bt->loadTextures(“LevelSel_8.png”,”LevelSel_16.png”,”“,UI_TEX_TYPE_PLIST); Upgrade_bt = (UIButton *)(layout_sel->getChildByTag(i+9)); sprintf(str,”num_%d.png”,idx+1); Upgrade_bt->loadTextures(str,str,UI_TEX_TYPE_PLIST); for(int j=0;j<6;++j) { layout_sel->getChildByTag(i+3+j)->setVisible(true); } for(int j=8;j>5+stars[idx+curoffset]-1;–j) { layout_sel->getChildByTag(i+j)->setVisible(false); } } else if(stars[idx+curoffset]==1) { //显示当前 Upgrade_bt->loadTextures(“LevelSel_14.png”,”LevelSel_15.png”,UI_TEX_TYPE_PLIST); for(int j=0;j<3;++j) { layout_sel->getChildByTag(i+3+j)->setVisible(true); } for(int j=3;j<6;++j) { layout_sel->getChildByTag(i+3+j)->setVisible(false); } } else { //锁定 Upgrade_bt->loadTextures(“LevelSel_19.png”,”LevelSel_20.png”,UI_TEX_TYPE_PLIST); for(int j=0;j<6;++j) { layout_sel->getChildByTag(i+3+j)->setVisible(false); } modeuw->setVisible(false); } if(stars[idx+curoffset]>=1) { //特殊模式 int levelId=curoffset+idx; int mode=(CDataSystem::GetLevelParanew(levelId,CGameDB::GetInstance()->GetTable(TABLE_ID_LEVEL_DATA)))->m_completeType; modeuw->setVisible(true); if(stars[idx+curoffset]==1) { sprintf(str,”LevelSel_1/level_mode_0_1.png”); modeuw->loadTexture(str); } else if(mode==GAME_MODE_NEARWEAPON)//近战模式 { sprintf(str,”LevelSel_1/level_mode_2_1.png”); modeuw->loadTexture(str); } else if(mode==GAME_MODE_REMOTEWEAPON)//远程模式 { sprintf(str,”LevelSel_1/level_mode_5_1.png”); modeuw->loadTexture(str); } else if(mode==GAME_MODE_SCATTER)//防御模式 { sprintf(str,”LevelSel_1/level_mode_3_1.png”); modeuw->loadTexture(str); } else if(mode==GAME_MODE_BALL)//铁球模式 { sprintf(str,”LevelSel_1/level_mode_4_1.png”); modeuw->loadTexture(str); } else if(mode==GAME_MODE_BOSS)//BOSS模式 { sprintf(str,”LevelSel_1/level_mode_1_1.png”); modeuw->loadTexture(str); } else { modeuw->setVisible(false); } } //背景图 sprintf(str,”LevelSel_1/level_bk_%d.png”,curoffset/10+1); ((UIImageView*)layout_sel->getChildByTag(i+1))->loadTexture(str); } } bool LevelSel::init() { ////////////////////////////////////////////////////////////////////////// for(int i=0;i<60;i++) { stars[i]=CArchive::GetInstance()->GetStageUnlock(i); if(stars[i]==1) curlevel=i; } if(!CGameDB::GetInstance()->GetTable(TABLE_ID_LEVEL_DATA)) CGameDB::GetInstance()->LoadTable(TABLE_ID_LEVEL_DATA,TABLE_TYPE_CSV); ////////////////////////////////////////////////////////////////////////// UILayer* baseUILayer=cocos2d::extension::UILayer::create(); Layout *widget = dynamic_cast