GameMenu.h
#ifdef WIN32
#pragma execution_character_set("utf-8")
#ifndef __GAMEMENU_SCENE_H__
#define __GAMEMENU_SCENE_H__
#include "cocos2d.h"
#include "ui/CocosGUI.h"
#include "SimpleAudioEngine.h"
#include <string.h>
#include <time.h>
#include "cocos-ext.h"
#include "Block.h"
#include "Level_01.h"
using namespace cocos2d::ui;
using namespace CocosDenshion;
USING_NS_CC_EXT;
USING_NS_CC;
class GameMenu : public cocos2d::Scene
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
// implement the "static create()" method manually
CREATE_FUNC(GameMenu);
};
#endif // __GAMEMENU_SCENE_H__
#endif
GameMenu.cpp
#ifdef WIN32
#pragma execution_character_set("utf-8")
#include "GameMenu.h"
#include "SimpleAudioEngine.h"
USING_NS_CC_EXT;
USING_NS_CC;
Scene* GameMenu::createScene(){
return GameMenu::create();
}
static void problemLoading(const char* filename){
printf("Error while loading: %s\n", filename);
printf("Depending on how you compiled you might have to add 'Resources/' in front of filenames in GameMenuScene.cpp\n");
}
bool GameMenu::init(){
if ( !Scene::init() ){
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
//auto sprite = Sprite::create("");
/*auto block = Block::create("HelloWorld.png",1,3,3);
block->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2));
block->setAnchorPoint(Point(0.5, 0.5));
this->addChild(block, 1);*/
//准备在该场景跳往Level_01场景
auto changeScene = MenuItemFont::create("GoToTheLevel_01", [=](Ref* sender) {
log("---------------------------------------");
auto level_01_Scene = Level_01::createScene(); //利用Level_01类创建一个场景
Director::getInstance()->pushScene(level_01_Scene); //替换原场景方式更换场景
return;
});//一个用户可以点击的菜单按钮
auto myMenu = Menu::create(changeScene, NULL);//菜单按钮需要挂载在菜单下
myMenu->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
this->addChild(myMenu, 1); //将菜单放在默认场景的第一层上
return true;
}
void GameMenu::menuCloseCallback(Ref* pSender){
Director::getInstance()->end();
}
#endif