前端之家收集整理的这篇文章主要介绍了
cocos2d单例,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
1.如何创建单例场景? .h #include "cocos2d.h" using namespace cocos2d; class Desk:public cocos2d::Layer { public: static cocos2d::Scene* createScene(); virtual bool init(); CREATE_FUNC(Desk); }; .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. } ********************************************************************************************* ********************************************************************************************* 2.如何在场景中
添加Layer? Director::getInstance()->setDepthTest(true);//创建游戏场景 Scene* LevelScene = Scene::create(); Desk* layer = LevelLayer::create(); layer->gsm = this; LevelScene->addChild(layer); //替换场景 Director::getInstance()->replaceScene(LevelScene); 3.如何进行场景切换? auto deskScene = Desk::createScene(); cocos2d::Director::getInstance()->replaceScene(deskScene); 4.如何在C++中定义公用类? SceneManagerControl* SceneManagerControl::share = NULL; SceneManagerControl* SceneManagerControl::getInstance(){ if (!share) { share = new SceneManagerControl(); CCLOG("share SceneManagerController"); } return share; }