Cocos2d-X realize scene switch, cocos2d-x scene Switch

Source: Internet
Author: User

Cocos2d-X realize scene switch, cocos2d-x scene Switch

To implement scenario conversion, You need to implement the scenario function. The scenario functions include:

RunWithScene: Running scenario

RunWithScene is a member function of the CCDirector class.

Function prototype: void runWithScene (CCScene * pScene)

Function parameter: * pScene (CCScene pointer)


ReplaceScene: Switch scenario

ReplaceScene is a member function of the CCDirector class.

Function prototype: void replaceScene (CCScene * pScene );

Function parameter: * pScene (CCScene pointer)


PushScene: Apply the scenario to the stack

PushScene is a member function of the CCDirector class.

Function prototype: void pushScene (CCScene * pScene );

Function parameter: * pScene (CCScene pointer)


PopScene: scene in the pop-up stack

PopScene is a member function of the CCDirector class.

Function prototype: void popScene (void );

Function parameter: No Parameter


Pause: pause scenario

Pause is a member function of the CCDirector class.

Function prototype: void pause (void)

Function parameter: No Parameter


Resume: continue to run the scenario

Resume is a member function of the CCDirector class.

Function prototype: void resume (void)

Function parameter: No Parameter


End: end scenario

End is a member function of the CCDirector class.

Function prototype: void end (void)

Function parameter: No Parameter


Create functions for engineering test scenarios:

Create a SceneMan project and add a SceneSecond class to the Project

Add the following code to the SceneSecond. h file:

# Ifndef _ SenceSecond_H _ # define _ SenceSecond_H _ // prevent code re-inclusion # include "cocos2d. h "USING_NS_CC; class SceneSecond: public CCLayer {public: // create a scenario static CCScene * scene (); // initialize the scenario bool init (); // menu callback function void menuCloseCallback (CCObject * pSender); CREATE_FUNC (SceneSecond) ;};# endif

Add the following code to SeceneSecond. cpp:

# Include "SceneSecond. h "# include" HelloWorldScene. h "CCScene * SceneSecond: scene () {// create a scenario CCScene * s = CCScene: create (); // create a layer SceneSecond * layer = SceneSecond :: create (); // Add the layer to the scenario s-> addChild (layer); // return the CCScene pointer return s;} bool SceneSecond: init () {// call the init function CCLayer: init () of the parent class first; // display the following text in the scenario: CCLabelTTF * label = CCLabelTTF: create ("SceneSecond", "abcde ", 36); // set the position (coordinates) label-> setPosition (CCPoint (100,200) of the displayed text; // Add the text to addChild (label) in the scenario ); CCSize visibleSize = CCDirector: shareddire()-> getVisibleSize (); CCPoint origin = CCDirector: sharedDirector ()-> getVisibleOrigin (); CCMenuItemImage * pCloseItem = custom :: create ("CloseNormal.png", "CloseSelected.png", this, menu_selector (SceneSecond: menuCloseCallback); pCloseItem-> setPosition (ccp (origin. x + visibleSize. width-pCloseItem-> getContentSize (). width/2, origin. y + pCloseItem-> getContentSize (). height/2); // create a menu CCMenu * pMenu = CCMenu: create (pCloseItem, NULL); pMenu-> setPosition (CCPointZero); this-> addChild (pMenu, 1); return true;} // menu callback function void SceneSecond: menuCloseCallback (CCObject * pSender) {// switch to the HelloWorld scenario CCDirector: shareddire () -> popScene ();}

Finally, modify the code in the HelloWorld. cpp file

# Include "HelloWorldScene. h "# include" SceneSecond. h "USING_NS_CC; CCScene * HelloWorld: scene () {// 'Scene 'is an autorelease object CCScene * scene = CCScene: create (); // 'player' is an autorelisted object HelloWorld * layer = HelloWorld: create (); // add layer as a child to scene-> addChild (layer ); // return the scene return scene;} // on "init" you need to initialize your instancebool HelloWorld :: Init () {// 1. super init first if (! CCLayer: init () {return false;} CCSize visibleSize = CCDirector: sharedDirector ()-> getVisibleSize (); CCPoint origin = CCDirector: shareddire () -> getVisibleOrigin (); // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelpolicobject CCMenuItemImage * pCloseItem = CCMenuItemImage: create ("example", "CloseSelected.png", this, menu_selector (HelloWorld: menuCloseCallback )); pCloseItem-> setPosition (ccp (origin. x + visibleSize. width-pCloseItem-> getContentSize (). width/2, origin. y + pCloseItem-> getContentSize (). height/2); // create menu, it's an autorelease object CCMenu * pMenu = CCMenu: create (pCloseItem, NULL); pMenu-> setPosition (CCPointZero ); this-> addChild (pMenu, 1); static int I = 1; char buf [256]; sprintf (buf, "Hello World % d", I ++ ); CCLabelTTF * pLabel = CCLabelTTF: create (buf, "Arial", 24); // position the label on the center of the screen pLabel-> setPosition (ccp (origin. x + visibleSize. width/2, origin. y + visibleSize. height-pLabel-> getContentSize (). height); // add the label as a child to this layer this-> addChild (pLabel, 1); // add "HelloWorld" splash screen "CCSprite * pSprite = CCSprite:: create ("HelloWorld.png"); // position the sprite on the center of the screen pSprite-> setPosition (ccp (visibleSize. width/2 + origin. x, visibleSize. height/2 + origin. y); // add the sprite as a child to this layer this-> addChild (pSprite, 0); return true ;}// the void HelloWorld function called after clicking the button:: menuCloseCallback (CCObject * pSender) {// switch to the SceneSecond scenario CCDirector: sharedDirector ()-> pushScene (SceneSecond: scene ());}

Compile the program: The execution result of the program is



Modify the program code and set the code in the void SceneSecond: menuCloseCallback (CCObject * pSender) function in SceneSecond. cpp.

  CCDirector::sharedDirector()->popScene();

Modify

CCDirector::sharedDirector()->replaceScene(HelloWorld::scene());


Set the code in the void HelloWorld: menuCloseCallback (CCObject * pSender) function in HelloWorldScene. cpp.

  CCDirector::sharedDirector()->pushScene(SceneSecond::scene());

Modify

CCDirector::sharedDirector()->replaceScene(SceneSecond::scene());

Program Execution result:




After modification, the number after HelloWorld will be accumulated because the scenario switching function used in the previous program is a stack function, and the stack and pressure stack operations will be performed during the scenario switching process, data is not accumulated, but data is accumulated when the replace function is used for switching scenarios.


Cocos2d-x scenario switching problem?

First Program Finance welcome page
CCDirector: sharedDirector ()-> runWithScene (Welcome to scene );
When switching a program, use the following code:
CCDirector: sharedDirector ()-> replaceScene (game interface scene );
This can be achieved.

Cocos2d-x can not achieve partial switching of scenarios, or Director can call multiple scenarios at the same time

What is the background with a CCLayer in a scenario?
Use CCLayerMultiplex as the Failover Layer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.