#include "cocos2d.h" USING_NS_CC; class MyScene : public cocos2d::Scene { public: static cocos2d::Scene* createScene(); virtual bool init(); CREATE_FUNC(MyScene); }; Scene* MyScene::createScene() { auto scene = Scene::create(); auto layer = MyScene::create(); scene->addChild(layer); return scene; } bool MyScene::init() { if (!Scene::init()) { return false; } // Add your game logic here. return true; } int main() { auto director = Director::getInstance(); director->runWithScene(MyScene::create()); return 0; }
void restartGame() { auto director = Director::getInstance(); auto newScene = MyScene::createScene(); director->replaceScene(newScene); }In the example above, we declare a function `restartGame()` that retrieves the `CCDirector` instance, creates a new scene using the `MyScene` class, and replaces the current scene with the new scene. Package library: Cocos2d-x