主菜单场景,主要实现进入游戏时的初始界面,它包括6个场景入口的菜单项:开始游戏,排行榜,帮助,设置,关于,退出
下面是MainLayer的头文件
#ifndef __MAIN_LAYER_H_ #define __MAIN_LAYER_H_ #include "cocos2d.h" #include "GameSceneManager.h" using namespace cocos2d; class MainLayer :public Layer//主界面主要包含6个菜单项 { public: virtual bool init(); int randNum();//产生随机数的方法 void initSound();//加载游戏音效的方法 void menuCallBack0(Ref* pSender);//切换到选择游戏场景 void menuCallBack1(Ref* pSender);//切换到排行榜场景 void menuCallBack2(Ref* pSender);//切换到游戏设置场景 void menuCallBack3(Ref* pSender);//切换到游戏帮助场景 void menuCallBack4(Ref* pSender);//切换到游戏关于场景 void menuCallBack5(Ref* pSender);//退出游戏的菜单回调方法 void initTitle();//更新标题 CREATE_FUNC(MainLayer); public: Sprite* backGround; NodeGrid* effectNode;//网格节点,用来实现3D水波涟漪特效 static bool musicFlag; static bool soundFlag; GameSceneManager* sceneManager; }; #endif
MainLayer.cpp文件,其中文件中的某些菜单项,暂时没有实现其功能,后续补充修改。
#include "MainLayer.h" #include "SimpleAudioEngine.h" bool MainLayer::musicFlag = true; bool MainLayer::soundFlag = true; bool MainLayer::init()//主菜单界面的初始化 { if (!Layer::init()) return false; //设置随机种子,该方法基本可以实现真正的随机 timeval psv; gettimeofday(&psv,nullptr); unsigned long int seed = psv.tv_sec * 1000 + psv.tv_usec / 1000; srand(seed); auto visibleSize = Director::getInstance()->getVisibleSize(); auto origin = Director::getInstance()->getVisibleOrigin(); int n = randNum();//获取随机数(0-4),产生随机背景 if (n % 2 == 0) backGround = Sprite::create("pic/morning.png"); else backGround = Sprite::create("pic/night.png"); backGround->setAnchorPoint(Vec2(0,0));//设置背景锚点在左下角 backGround->setPosition(origin.x,origin.y + visibleSize.height - backGround->getContentSize().height); this->addChild(backGround,0); if (MainLayer::musicFlag) CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("sounds/playscene.mp3",true); auto floor = Sprite::create("pic/floor.png"); floor->setAnchorPoint(Vec2(0,0)); floor->setPosition(origin); this->addChild(floor); floor->runAction(RepeatForever::create(Sequence::create(MoveTo::create(0.5f,Vec2(-120,0)),MoveTo::create(0,Vec2(0,nullptr))); //菜单项加载 auto menuItemStart = MenuItemImage::create("button/start.png","button/start_off.png",CC_CALLBACK_1(MainLayer::menuCallBack0,this)); menuItemStart->setPosition(Vec2(150,600)); auto menuItemRank = MenuItemImage::create("button/rank.png","button/rank_off.png",CC_CALLBACK_1(MainLayer::menuCallBack1,this)); menuItemRank->setPosition(Vec2(390,600)); auto menuItemSet = MenuItemImage::create("button/set.png","button/set_off.png",CC_CALLBACK_1(MainLayer::menuCallBack2,this)); menuItemSet->setPosition(Vec2(150,450)); auto menuItemHelp = MenuItemImage::create("button/help.png","button/help_off.png",CC_CALLBACK_1(MainLayer::menuCallBack3,this)); menuItemHelp->setPosition(Vec2(390,450)); auto menuItemAbout = MenuItemImage::create("button/about.png","button/about_off.png",CC_CALLBACK_1(MainLayer::menuCallBack4,this)); menuItemAbout->setPosition(Vec2(150,300)); auto menuItemExit = MenuItemImage::create("button/exit.png","button/exit_off.png",CC_CALLBACK_1(MainLayer::menuCallBack5,this)); menuItemExit->setPosition(Vec2(390,300)); auto mn = Menu::create(menuItemStart,menuItemRank,menuItemSet,menuItemHelp,menuItemAbout,menuItemExit,nullptr); mn->setPosition(Vec2::ZERO); this->addChild(mn,10); effectNode = NodeGrid::create(); this->addChild(effectNode,10);//加入网格节点 auto title = Sprite::create("pic/title.png"); title->setPosition(Vec2(270,800)); effectNode->addChild(title);//将精灵添加到网格节点中 Director::getInstance()->setDepthTest(false);//关闭深度检测 effectNode->runAction(RepeatForever::create(Ripple3D::create(2.0f,Size(32,24),Vec2(270,800),360,4,5))); return true; } int MainLayer::randNum() { int num = random(0,4);//根据产生的数,确定要加载的背景图片 return num; } void MainLayer::initTitle() { auto title = Sprite::create("pic/title.png"); title->setPosition(270,800); this->addChild(title,10); this->removeChild(effectNode); } void MainLayer::menuCallBack0(Ref* pSender) { } void MainLayer::menuCallBack1(Ref* pSender) { initTitle(); sceneManager->goToRankBirdScene(); } void MainLayer::menuCallBack2(Ref* pSender) { } void MainLayer::menuCallBack3(Ref* pSender) { } void MainLayer::menuCallBack4(Ref* pSender) { } void MainLayer::menuCallBack5(Ref* pSender) { }