一切之前还是说抱歉,因为本人的确实不会搞美工做的事情,所以素材的图片丑的要死。
先看此界面的效果:
然后源码是:
MenuScene.h
/** * 主菜单场景的声明 */ #ifndef __MENU_SCENE_H__ #define __MENU_SCENE_H__ #include "cocos2d.h" USING_NS_CC; /** * 定义一个MenuLayer类继承自Layer类 */ class MenuLayer : public Layer { public: /** * 构造函数 */ MenuLayer(); /** * 析构函数 */ virtual ~MenuLayer(); /** * 创建当前场景并加入MenuLayer层 */ static Scene* createScene(); /** * 初始化MenuLayer层 */ virtual bool init(); /** * 去主游戏场景 */ void goToMainGameScene(Ref* pSender); /** * 创建MenuLayer层 */ CREATE_FUNC(MenuLayer); }; #endif
MenuScene.cpp
/** * 主菜单场景的实现 */ #include "MenuScene.h" #include "VisibleRect.h" /** * 构造函数,一般用来初始化变量 */ MenuLayer::MenuLayer(){} /** * 析构函数,一般用来释放内存 */ MenuLayer::~MenuLayer(){} /** * 创建场景,加入MenuLayer层 */ Scene* MenuLayer::createScene() { Scene* scene = Scene::create(); MenuLayer *layer = MenuLayer::create(); scene->addChild(layer); return scene; } /** * 初始化 MenuLayer */ bool MenuLayer::init() { if (!Layer::init()) { return false; } //加载资源 SpriteFrameCache::getInstance()->addSpriteFramesWithFile("imageResource.plist","imageResource.png"); //加入背景页 Sprite *bg = Sprite::createWithSpriteFrameName("menu_bg.png"); bg->setPosition(VisibleRect::center()); this->addChild(bg); //添加游戏名称包括闪光效果 Sprite *gameName = Sprite::createWithSpriteFrameName("title.png"); Size clipSize = gameName->getContentSize(); Sprite* spark = Sprite::createWithSpriteFrameName("shark.png"); spark->setPosition(-clipSize.width,0); //裁剪 ClippingNode* clippingNode = ClippingNode::create(); clippingNode->setAlphaThreshold(0); clippingNode->setContentSize(clipSize); clippingNode->setPosition(VisibleRect::center() + Point(0,90)); clippingNode->setStencil(gameName); clippingNode->addChild(gameName,1); clippingNode->addChild(spark,2); this->addChild(clippingNode); //闪光的动作 MoveTo* moveAction = MoveTo::create(2.0f,Point(clipSize.width,0)); MoveTo* moveBackAction = MoveTo::create(2.0f,Point(-clipSize.width,0)); Sequence* seq = Sequence::create(moveAction,moveBackAction,NULL); RepeatForever* repeatAction = RepeatForever::create(seq); spark->runAction(repeatAction); //开始按钮 Sprite *beginNomal = Sprite::createWithSpriteFrameName("begin_normal.png"); Sprite *beginPressed = Sprite::createWithSpriteFrameName("begin_pressed.png"); MenuItemSprite* startGameItem = MenuItemSprite::create(beginNomal,beginPressed,CC_CALLBACK_1(MenuLayer::goToMainGameScene,this)); startGameItem->setPosition(VisibleRect::center().x,VisibleRect::center().y); startGameItem->setTag(10); //继续按钮 Sprite *goOnNomal = Sprite::createWithSpriteFrameName("go_on_normal.png"); Sprite *goOnPressed = Sprite::createWithSpriteFrameName("go_on_pressed.png"); MenuItemSprite* goOnItem = MenuItemSprite::create(goOnNomal,goOnPressed,this)); goOnItem->setPosition(VisibleRect::center().x,VisibleRect::center().y * 0.75f); goOnItem->setTag(11); //菜单 Menu* menu = Menu::create(startGameItem,goOnItem,NULL); menu->setPosition(Point::ZERO); this->addChild(menu,1); return true; } /** * 去主游戏界面 */ void MenuLayer::goToMainGameScene(Ref* pSender){ Scene* scene = nullptr; Node* node = (Node*)pSender; int tag = node->getTag(); switch (tag){ case 10:{ //重新开始的设置配置 break; } case 11:{ //继续关卡的数据配置 break; } } //进入新场景,这里因为还没写所以先空着 //if (scene){ // Director::getInstance()->replaceScene(TransitionFade::create(0.5,scene)); //} }
然后别忘了在LoadScene.cpp里面加入以下代码用于场景切换
#include "MenuScene.h"
和
//加载完成去下一个场景 Scene* scene = MenuLayer::createScene(); Director::getInstance()->replaceScene(scene);
位置如图: