1.如何在C++中单例公用类?
1).h
public:
static SceneManagerControl* getInstance();
static SceneManagerControl * share;
2).cpp
SceneManagerControl* SceneManagerControl::share = NULL;
SceneManagerControl* SceneManagerControl::getInstance(){if (!share)
{
share = new SceneManagerControl();
CCLOG("share SceneManagerController");
}
return share;
}
2.如何创建单例场景?
1).h#include "cocos2d.h"
using namespace cocos2d;
class Desk:public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
CREATE_FUNC(Desk);
};
2).cpp
#include "DeskScene.h"
cocos2d::Scene* Desk::createScene(){
auto scene = Scene::create();
auto layer = Desk::create();
scene->addChild(layer);
return scene;
}
bool Desk::init(){
if (!Layer::init())
{
return false;
}
//add layer or btn and so on.
}
*********************************************************************************************
*********************************************************************************************
3.如何在场景中添加Layer?
Director::getInstance()->setDepthTest(true);//创建游戏场景
Scene* LevelScene = Scene::create();
Desk* layer = LevelLayer::create();
layer->gsm = this;
LevelScene->addChild(layer);
//替换场景
Director::getInstance()->replaceScene(LevelScene);
4.如何进行场景切换?
auto deskScene = Desk::createScene();
cocos2d::Director::getInstance()->replaceScene(deskScene);