How to Create a CCScene cocos2dX UI control and 24 switching modes

Source: Internet
Author: User

Today, let's look at the scenarioCCSceneCreation principle and switching mode,


First, let's see what an empty scenario looks like:




Let's take a look at the effect: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + ybbSssO709A6PC9wPgo8cD48aW1nIHNyYz0 = "http://www.2cto.com/uploadfile/Collfiles/20140603/2014060308472330.jpg" alt = "\">


You are impatient and want to explain them one by one (Narrator: What are you doing today)


Create a HelloWorld inherited from the CCLayer, and then use HelloWorld to provide a static function that generates the CCScene pointer, get the pointer of the scenario, and load the scenario.

Let's take a detailed look at what each function has done.


CCScene * HelloWorld: scene ()

Creates a scenario

A layer is created based on HelloWorld.

Add layers to scenarios

Return scenario pointer


Bool HelloWorld: init ()

Determines whether the parent class has been initialized successfully. If not, end the initialization operation.


In general, CCScene is equivalent to a container. Although it inherits from CCNode, it does nothing. It does not add member variables and methods on the basis of it, but only restructured init and adds layers to the scene.


Therefore, we need to perform various operations in the init function, and then add it to the layer using addChild. We will then let CCDirector call runWithScene () to play the scenario, the genie we added can be displayed on the screen.




Now let's take a look at the most important part of today, scenario switching.

First, let's take a look at the effects of scenario switching:

[1]: CCTransitionCrossFade: create (time, target scenario );

// Fade down to another scenario

[2]: CCTransitionFade: create (time, target scenario );

// This scenario fades out and then appears in another scenario.

[3]: CCTransitionFadeBL: create (time, target scenario );

// This scenario disappears from the upper right corner to the lower left corner to another scenario.

[4]: CCTransitionFadeDown: create (time, target scenario );

// This scenario disappears from top to bottom to another scenario

[5]: CCTransitionFadeTR: create (time, target scenario );

// This scenario disappears from the lower left corner to the upper right corner to another scenario

[6]: CCTransitionFadeUp: create (time, target scenario );

// This scenario disappears from bottom to top to another scenario

[7]: CCTransitionFlipAngular: create (time, target scenario, style );

// In this scenario, the flip disappears to another scenario (in the top oblique corner)

// Style (can be left empty ):

// KCCTransitionOrientationLeftOver (Flip left to right)

// KCCTransitionOrientationRightOver (FLIP right to left)

[8]: CCTransitionFlipX: create (time, target scenario, style );

// In this scenario, the flip disappears to another scenario (X axis)

// Style (can be left empty ):

// KCCTransitionOrientationLeftOver (Flip left to right)

// KCCTransitionOrientationRightOver (FLIP right to left)

[9]: CCTransitionFlipY: create (time, target scenario );

// In this scenario, the flip disappears to another scenario (Y axis)

// Style (can be left empty ):

// KCCTransitionOrientationUpOver (flip up)

// KCCTransitionOrientationDownOver (flip up and down)

[10]: CCTransitionJumpZoom: create (time, target scenario );

// This scenario disappears and then another scenario appears

[11]: CCTransitionMoveInB: create (time, target scenario );

// In another scenario, how does one extract the stuffing from below? http://www.bkjia.com/kf/yidong/wp/ "Target =" _ blank "class =" keylink "> release + release/B7dK7s6G + sNPJ1fvM5bTT1/release + release/release + 8zltNPJz8Pms/bp11_vcd4kpha + release + az9s /queue + queue/LHsLetoaM8L3A + queue/bp1133 vcd4kpha + queue/queue + sLP2z9Y8L3A + queue/queue + s6G + queue/queue /samples + samples/seqzob6wKTs8L3A + samples + s6G + sMuzyrHV68/samples + samples/seqzob6wKTs8L3A + samples + s6G + large + sLfFtPM8L3A + large + xvrOhvrDP8s/large + x6rOhvrApOzwvcD4KPHA + large + 2 NDOyc/Pws/release + 2 NDOyc/PwrP2z9Y8L3A + release + 2NDO1/PT0s/release + 2NDO1/release + release/pz/release + release + xvrOhvrC3rdeqz/release + release/R + cq9o6i/release + release/release + sCzR + cq9KTs8L3A + release + s6G + sLet16rP + 8qntb3B7dK7s6G + sKOoWdbho6k8L3A + pushed/pushed + pushed/yyc + pushed/pushed + usPByywgztLDx8/pushed = "brush: java; "> # ifndef _ SCENE2_H __# define _ SCENE2_H __# include" cocos2d. h "class scene2: public cocos2d: CCLayer {public: // initialize a scenario, return TRUE if successful, and return FALSEvirtual bool init () If failed; // create a scenario, returns the instance pointer of the class. To put it bluntly, it returns a scene pointer static cocos2d: CCScene * scene (); // creates a layer CREATE_FUNC (scene2 );}; # endif/_ SCENE2_H __

# Include "scene2.h" USING_NS_CC; CCScene * scene2: scene () {// create a scene object and return it CCScene * scene = CCScene: create (); // create a layer scene2 * layer = scene2: create (); // Add the layer to the scene-> addChild (layer); return scene;} bool scene2 :: init () {if (! CCLayer: init () {return false;} return true ;}

In this way, a new scenario is created.


In the original scenario, we created a background image, a button, and an amount. I used the close button in the program to change the function content. Hey, you know (Narration: you are at a low cost)

CCSprite* bg = CCSprite::create( "HelloWorld.png");bg->setPosition( ccp( visibleSize.width / 2, visibleSize.height / 2));addChild( bg);CCMenuItemImage *pCloseItem = CCMenuItemImage::create("CloseNormal.png","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 objectCCMenu* pMenu = CCMenu::create(pCloseItem, NULL);pMenu->setPosition(CCPointZero);this->addChild(pMenu, 1);

The key is to modify the function of closing the button and directly use scene2's scene () function to create a scenario. Use the director class to directly switch to our new scenario.

CCScene* myScene = scene2::scene();CCDirector::sharedDirector()->replaceScene( myScene);

Of course, we have to introduce the header file of scene2.

#include "scene2.h"

Modify the scene2 Image Based on the sample image, and then implement scene2:

CCLabelTTF* ttf = CCLabelTTF::create( "SCENE_2", "Arial", 40);ttf->setPosition( ccp( visibleSize.width / 2, visibleSize.height / 2));addChild( ttf);CCMenuItemImage *pCloseItem = CCMenuItemImage::create("CloseNormal.png","CloseSelected.png",this,menu_selector(scene2::menuCloseCallback));pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,origin.y + pCloseItem->getContentSize().height/2));// create menu, it's an autorelease objectCCMenu* pMenu = CCMenu::create(pCloseItem, NULL);pMenu->setPosition(CCPointZero);this->addChild(pMenu, 1);

Modify and close the function. In this switching scenario, we chose to use a special effect for the sliding switching scenario:

void scene2::menuCloseCallback(CCObject* pSender){CCScene* myScene = HelloWorld::scene();CCTransitionScene* tx = CCTransitionMoveInB::create( 2, myScene);CCDirector::sharedDirector()->replaceScene( tx);}



Let's look at the effect. Scenario 1 is switched to Scenario 2:

You can switch it directly without any effect.


Let's take a look at scenario 2 and switch to scenario 1:

Pull the scenario from the following:



Now we are here today. I hope you can take a look at the effects of these special effects.

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.