cocos2d-x 4.0 learning road (seven) scene switching

Scene switching

According to the previous article, we established our own MyHelloWorldScene. So how to switch from HelloWorldScene to MyHelloWorldScene?

it's actually really easy. We want to switch scenes, then there must be a trigger point, we use the close button in the lower right corner of HelloWorld to achieve it.

What we need to do is to press the close button instead of closing the window but switching to MyHelloWorldScene. So let's take a look at the menuCloseCallback () function of HelloWorldScene.cpp. Just remove the closing process and add a sentence. (Note: Do n’t forget to add the MyHelloWorldScene.h header file)

void HelloWorld::menuCloseCallback(Ref* sender)
{
    //Director::getInstance()->end();
    Director::getInstance()->replaceScene(MyHelloWorld::createScene());
}

As you can see, I changed the Label text and pictures in MyHelloWorld so that it looks clearer.

Special effect

Such a direct transformation is a bit too earthy, how to add some transformation effects. Cocos2dx has prepared the function for us, just need to become the following code to be OK.

void HelloWorld::menuCloseCallback(Ref* sender)
{
    //Director::getInstance()->end();
    Director::getInstance()->replaceScene(TransitionSlideInT::create(2.0f, MyHelloWorld::createScene()));
}

Looking at the effect, do you feel the initial blockbuster?

TransitionSlideInT is also a scene class, which creates this kind of scene with a sliding effect through the Create function.

It has two parameters,

float f: time for scene switching, in seconds

Scene * scene: the target scene you want to switch

Cocos2dx not only provides this special effect, but also many others,

For example: TransitionJumpZoom: The original scene zooms out and the new scene zooms in.

Then there is TransitionFadeUp: the blind effect from bottom to top:

There are not many other examples, you can check the official API . Those transitions start with switching scenes.

Scene advancement and pop-up

Use replaceScene to switch scenes. After the new scene is displayed, the old scene resources will be released. This is very necessary because a scene can sometimes be very large, so the resources of the old scene must be released after switching scenes.

Then sometimes, we don't want the old scene to be released, but want to switch back and forth between the old and new scenes. This uses the scene to advance and pop up.

Change replaceScene to pushScene:

// HelloWorldScene.cpp
void HelloWorld::menuCloseCallback(Ref* sender)
{
    //Director::getInstance()->end();
    Director::getInstance()->pushScene(TransitionSplitCols::create(2.0f, MyHelloWorld::createScene()));
}

Then in MyHelloWorldScene, make the following changes:

// MyHelloWorldScene.h
void backToScene(Ref* sender);

// MyHelloWorldScene.cpp
    auto closeItem = MenuItemImage::create(
        "CloseNormal.png",
        "CloseSelected.png",
        CC_CALLBACK_1(MyHelloWorld::backToScene, this));

void MyHelloWorld::backToScene(Ref* sender)
{
    Director::getInstance()->popScene();
}

The popScene and pushScene functions are corresponding. Use the popScene () function to return to the original scene, and the scene resources of MyHelloWorld will be released.

Published 104 original articles · Like8 · Visit 210,000+

Guess you like

Origin blog.csdn.net/sunnyboychina/article/details/105069922