Study notes Cocos2d-x (3.1) Scene scene and a scene cut

Introduction 1. Scene

The game we see / do not see all the elements are displayed on the Scene scene.

We can compare the scene on the ground did not cover cartons, cardboard boxes piled layer Layer is glass, Sprite and other elements painted on glass Layer, so that we will be able to look down from the carton to see this scene. When a scene cut, a switch to a different carton.

In summary, managing director of N scenes, scene management N layers, layer management, etc. N sprites small elements, each sprite with N action Timer and so on.

Scene Scene directly inherited Node.

2. Scene 类

Anchor default constructor (0.5, 0.5). The main method is to create methods.

create a new method scene and call the init method, init acquired screen size, call initWithSize, execution setContentSize set the scene size.

createWithSize method empathy.

We can customize a inherited Scene scene in the custom scene, override the init method and onEnter onExit and other methods to achieve our own scene.

3. Director 与 Scene

Director can manage scenes, scenes of execution and achieve switching.

There are two scenarios director pointer variables.

    Scene *_runningScene;
    Scene *_nextScene;

runningScene is is in front of the scene. nextScene if any, will be in the next frame to be runningScene scene.

    Vector<Scene*> _scenesStack;

Scene pointer is stored in the Vector class director scenesStack the container, can know from the name stored by the stack.

  bool _sendCleanupToScene;
  bool isSendCleanupToScene() { return _sendCleanupToScene; }

bool variable sendCleanupToScene determine whether the replaced scene cleanup receive information. When a new scene is replace the old scene cleanup receive information; new scene is push the old scene will not be cleaned up.

Director in the scene related methods:

    void runWithScene(Scene *scene);
    void pushScene(Scene *scene);
    void popScene();
    void popToRootScene();
    void popToSceneStackLevel(int level);
    void replaceScene(Scene *scene);
    void end();
    void pause();
    void resume();
    void restart();
    void stopAnimation();
    void startAnimation();
    void drawScene();

4. Scene in Director

In the Application run method, first game is AppDelegate applicationDidFinishLaunching initialization method of execution. This method calls the init method to write our own scene, after obtaining the scene pointer scene, perform director-> runWithScene (scene).

runWithScene(scene)

void Director::runWithScene(Scene *scene)
{
    CCASSERT(scene != nullptr, "This command can only be used to start the Director. There is already a scene present.");
    CCASSERT(_runningScene == nullptr, "_runningScene should be null");

    pushScene(scene);
    startAnimation();
}

This method requires the director runningScene empty. Continue to look at.

- pushScene(scene)

The first method sendCleanupToScene flag is set false. Then execute:

    _scenesStack.pushBack(scene);
    _nextScene = scene;

Scene added to the stack in the scene, and the scene is set nextScene our custom.

- drawScene()

drawScene mainLoop method, the scheduler then performs update:

    IF (_nextScene) 
    { 
        setNextScene (); // nextScene assigned runningScene, after nextScene empty 
    }

- setNextScene()

Briefly, the method to nextScene assigned to the runningScene, if not at this time of transition runningScene scene, will perform two methods of enter.

To perform the method in the case of the director of the current frame nextScene existence, into the next scene.

The scene cut

Director scene cut is divided into two methods:

  1. replaceScene: new scene replace the original scene, the original scene will be cleanup. The new scene pushed the stack.

  2. pushScene popScene: push the new scene replace the original scene, the original scene but "hidden", pop method would "restore" the original scene. The new scene is not pushed onto the stack.

- replaceScene(Scene *scene)

The parameterized scene replace the current scene. Parameters scene is pressed onto the stack, and becomes nextScene.

1. If runningScene is empty, execution runWithScene (scene), the method ends.

2. If the parameter is equal scene nextScene, the end of the method, without replacing scene.

3. If there nextScene, nextScene first determines whether the running state (after the onEnter, before onExit), and if so, its implementation onExit. NextScene execution of cleanup () and Fu nullptr, clean up nextScene.

4. _sendCleanupToScene = true,因为replaceScene场景切换需要cleanup原先的runningScene。

_sendCleanupToScene唯一的用处是在每一帧setNextScene()之中。在将nextScene赋给runningScene之前,需要判断该值,为true时,对runningScene执行cleanup。

5. scenesStack栈顶的scene替换为参数scene,并且nextScene指向参数scene。

- pushScene(Scene *scene)

代码精简后很简单。push方法将参数scene压入栈顶且成为nextScene。

void Director::pushScene(Scene *scene)
{
    _sendCleanupToScene = false;
    _scenesStack.pushBack(scene);
    _nextScene = scene;
}

_sendCleanupToScene置false,因为此种场景变换不要对原场景cleanup。

- popScene()

简要的说,pop方法将栈顶scene作为nextScene,_sendCleanupToScene置true,导致当前runningScene将被cleanup。

void Director::popScene(void)
{
    CCASSERT(_runningScene != nullptr, "running scene should not null");
    _scenesStack.popBack();
    ssize_t c = _scenesStack.size();
    if (c == 0)
    {
        end();
    }
    else
    {
        _sendCleanupToScene = true;
        _nextScene = _scenesStack.at(c - 1);
    }
}

在从栈中popBack之后,若场景栈为空,此时将无法恢复原场景,说明pop没有和push搭配使用,导致导演在下一帧结束程序。

6. 过渡场景在场景切换中的使用

请看本文:Cocos2d-x 学习笔记(3.2) TransitionScene 过渡场景

Guess you like

Origin www.cnblogs.com/deepcho/p/cocos2dx-scene.html