After more than a day of work, I finally completed this function. In fact, adding a new scenario does not take much time, and the time is mainly spent on switching to another scenario, the main reason is that an error occurs during compilation, which cannot be solved. Later, I found out the problem after checking the information and correct the error, I realized what I wanted and looked at the Freedom of switching in that scenario and the beauty of various special effects during the switching process.
Let's start my journey:
First, create a new scenario. In fact, you can refer to the helloworld scenario to create your own scenario. Of course, the features you implement in the new scenario are determined by yourself. The following is my new scenario code:
Secondscene. h:
1 #include "cocos2d.h" 2 3 class SecondScene : public cocos2d::Layer 4 { 5 public: 6 static cocos2d::Scene* createScene(); 7 virtual bool init(); 8 void menuCloseCallback(cocos2d::Ref* pSender); 9 CREATE_FUNC(SecondScene);10 };
1 #include "SecondScene.h" 2 #include "HelloWorldScene.h" 3 USING_NS_CC; 4 5 Scene* SecondScene::createScene() 6 { 7 auto scene = Scene::create(); 8 auto layer = SecondScene::create(); 9 scene->addChild(layer);10 return scene;11 }12 13 bool SecondScene::init()14 {15 16 if ( !Layer::init() )17 {18 return false;19 }20 21 Size visibleSize = Director::getInstance()->getVisibleSize();22 Vec2 origin = Director::getInstance()->getVisibleOrigin();23 auto closeItem = MenuItemImage::create(24 "CloseNormal.png",25 "CloseSelected.png",26 CC_CALLBACK_1(SecondScene::menuCloseCallback, this));27 28 closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,29 origin.y + closeItem->getContentSize().height/2));30 31 auto menu = Menu::create(closeItem, NULL);32 menu->setPosition(Vec2::ZERO);33 this->addChild(menu, 1);34 35 auto label = LabelTTF::create("Hello World world", "Arial", 24);36 37 label->setPosition(Vec2(origin.x + visibleSize.width/2,38 origin.y + visibleSize.height - label->getContentSize().height));39 40 this->addChild(label, 1);41 42 auto sprite = Sprite::create("HelloWorld.png");43 44 sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));45 46 this->addChild(sprite, 0);47 48 return true;49 }50 51 52 void SecondScene::menuCloseCallback(Ref* pSender)53 {54 CCScene* s = HelloWorld::createScene(); 55 Director::sharedDirector()- >replaceScene(CCTransitionFlipX::create(2.0f,s)); 56 }
Modify helloworld: menuclosecallback (ref * psender) in helloworld. cpp)
1 void HelloWorld::menuCloseCallback(Ref* pSender)2 {3 CCScene* secondScene = SecondScene::createScene(); 4 Director::sharedDirector()->pushScene(CCTransitionJumpZoom::create(2.0f,secondScene)); 5 }
At this point, the new scenario is created. The following is the special effect of switching the compilation and running to the new scenario.
During the compilation process, I encountered the following problem, which took a long time to solve.
JNI/.../../classes/helloworldscene. cpp: 118: Error: Undefined reference to 'secondscene: createscene ()'
Clang ++: Error: Linker command failed with exit code 1 (use-V to see Invocation)
Make: *** [OBJ/local/armeabi/libcocos2dcpp. So] Error 1
Solution: register for each new scenario, find the Android. mk file under the JNI --> classes directory, and add the new scenario file secondscene. cpp.
1 LOCAL_SRC_FILES := hellocpp/main.cpp 2 ../../Classes/AppDelegate.cpp 3 ../../Classes/HelloWorldScene.cpp 4 ../../Classes/SecondSce
After the code is re-compiled, click the scenario menu to switch the effect of the special effect.
How to add new scenarios and switch new scenarios (including special effects) to Cocos2d-x)