下面我们新建一个关卡场景类代码如下:
class ChoiceScene : public cocos2d::CCLayer { public : virtual bool init(); CREATE_FUNC(ChoiceScene); static cocos2d::CCScene *scene(); };
CCScene *ChoiceScene::scene() { CCScene *scene = CCScene::create(); ChoiceScene *layer = ChoiceScene::create(); if (scene && layer) { scene->addChild(layer); } return scene; }
// 1. 初始化父类,并设置Android返回键和菜单键 if (!CCLayer::init()) { return false; } setKeypadEnabled(true);相应的要添加相应函数 virtual void keyBackClicked(void);和virtual void keyMenuClicked(void);
2.添加一个黑色背景,并进行比例缩放
CCSize szWin = CCDirector::sharedDirector()->getWinSize(); CCSprite *pSprite = CCSprite::create("Black.png"); pSprite->setPosition(ccp(szWin.width / 2, szWin.height / 2)); CCSize szSprite = pSprite->getContentSize(); pSprite->setScaleX(szWin.width / szSprite.width); pSprite->setScaleY(szWin.height / szSprite.height); this->addChild(pSprite, 0);
3.设置开始游戏、上一关,下一关按钮以及关卡数显示的图片
以及设置他们的缩放比例和位置
const char *szImgs[4] = { "paddle/btn_play.png", "paddle/btn_rewind.png", "paddle/btn_fast_forward.png", "fonts/fps_images.png" }; float fSettings[4][4] = { { 20.0f / 480, 16.0f / 320, 280.0f / 480, 160.0f / 320 }, { 20.0f / 480, 16.0f / 320, 200.0f / 480, 160.0f / 320 }, { 20.0f / 480, 16.0f / 320, 240.0f / 480, 160.0f / 320 }, { 20.0f / 480, 22.0f / 320, 280.0f / 480, 172.0f / 320 } };
4.分别对开始游戏,上一关,下一关按钮的图片加载上来显示到合适位置
for (int i = 0; i < 3; ++i) { float* fSetting = fSettings[i]; CCTexture2D* paddleTexture = CCTextureCache::sharedTextureCache()->addImage(szImgs[i]); CCSprite* pPaddle = CCSprite::createWithTexture(paddleTexture); CCSize szBtn = pPaddle->getContentSize(); pPaddle->setScaleX(szWin.width / szBtn.width * fSetting[0]); pPaddle->setScaleY(szWin.height / szBtn.height * fSetting[1]); pPaddle->setPosition(ccp(szWin.width * fSetting[2], szWin.height * fSetting[3])); addChild(pPaddle); }
int i = 3; ccColor3B color = { 0, 0, 0 }; float* fSetting = fSettings[i]; CCLabelAtlas* label1 = CCLabelAtlas::create("1", szImgs[i], 16, 32, '.'); CCSize sz = label1->getContentSize(); label1->setAnchorPoint(ccp(0.5f, 0.5f)); label1->setScaleX(szWin.width / sz.width * fSetting[0]); label1->setScaleY(szWin.height / sz.height * fSetting[1]); label1->setPosition(ccp(szWin.width * fSetting[2], szWin.height * fSetting[3])); label1->setColor(color); addChild(label1, 0);
进入HelloWorld.cpp修改以下函数:
void HelloWorld::menuPlayGameCallback(CCObject* pSender) { //开始菜单被点击时调用 CCScene* pScene = ChoiceScene::scene(); CCDirector::sharedDirector()->pushScene(pScene); return; }
完整代码下载地址:
http://download.csdn.net/detail/yincheng01/6721463