Study notes Cocos2d-x (3.2) TransitionScene transient scene and a scene change

1 Introduction

Transition scene TransitionScene direct successor to the scene Scene. To achieve "transition" effect in the scene switching process, rather than a window suddenly show another scene in the next frame.

2. create

Constructor:

TransitionScene::TransitionScene()
: _inScene(nullptr)
, _outScene(nullptr)
, _duration(0.0f)
, _isInSceneOnTop(false)
, _isSendCleanupToScene(false)
{
}

create method has two parameters: float t, Scene * scene. Call initWithDuration, passing these two parameters.

Parameters scene can not be empty.

1. member variable as a parameter _duration time t, _inScene member variable as a parameter scene. Transition scenario refers to an argument scene, the scene parameters to retain.

2. Director execution getRunningScene, the scene is currently being performed as _outScene.

3. If the scenario currently being executed is empty, Scene is called :: create ()? Create an empty scene, empty scene as _outScene.

4. _outScene scene point is referenced, _outScene performed retain.

5. Perform sceneOrder (), and determines the drawing order inScene of outScene. The method may be overridden by subclasses, but the member variable set value _isInSceneOnTop true / false.

3. onEnter

onEnter method of execution of the scenario at the entrance window of each scene.

TransitionScene subclass will override the onEnter method, a method of rewriting after the body will first call the parent class TransitionScene of onEnter.

1. Call the parent class Scene onEnter first method, the method is onEnter Scene parent class Node is actually executed.

2. The director of the event dispatcher execution setEnabled (false), to stop the event dispatcher work.

3. _outScene-> onExitTransitionDidStart () To exit the scenario execution onExitTransitionDidStart.

4. _inScene-> the onEnter scene () appears to be the execution onEnter.

In short, at the beginning of the transition, to exit ready to quit, began to appear appear.

4. onExit()

onExit method of execution scene when the scene each time you exit the window.

1. Call the parent class Scene onExit first method, the method is onExit Scene parent class Node is actually executed.

2. The director of the event dispatcher execution setEnabled (true), the event dispatcher can distribute the event.

3. _outScene-> onExit (); To exit the scenario execution onExit, completely out.

4. _inScene-> Scene onEnterTransitionDidFinish () appears to be the execution onEnterTransitionDidFinish.

In short, at the end of the transition, to withdraw from the completion of exit, to enter into the completion. At this point, only a window to enter the scene we set.

5. draw(...)

Source method of rendering a scene transition:

void TransitionScene::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
    Scene::draw(renderer, transform, flags);

    if( _isInSceneOnTop ) {
        _outScene->visit(renderer, transform, flags);
        _inScene->visit(renderer, transform, flags);
    } else {
        _inScene->visit(renderer, transform, flags);
        _outScene->visit(renderer, transform, flags);
    }
}

As can be seen, the drawing and the drawing order inScene outScene according _isInSceneOnTop decision.

The default constructor false. In the create method calls sceneOrder () settings. Subclasses can override sceneOrder methods to achieve different scenarios to enter and to exit the scene rendering order different subclasses TransitionScene.

6. 子类 TransitionJumpZoom

TransitionScene is the parent class for all transition scenarios, and each seed type is a kind of different transition effects scene.

The sub-category TransitionJumpZoom example for learning.

The scene transition constructor and destructor removed only two methods. The rewrite onEnter scene transition method, described in the method comprises a logic transition effects.

- TransitionJumpZoom::onEnter()

1. First run a parent TransitionScene :: onEnter (). Let outScene (runningScene) ready to quit.

2. Set inScene and outScene initial properties during the transition began.

    _inScene->setScale(0.5f);
    _inScene->setPosition(s.width, 0);
    _inScene->setAnchorPoint(Vec2(0.5f, 0.5f));
    _outScene->setAnchorPoint(Vec2(0.5f, 0.5f));

3. Create three movements. There is a jump and zoom

    ActionInterval *jump = JumpBy::create(_duration/4, Vec2(-s.width,0), s.width/4, 2);
    ActionInterval *scaleIn = ScaleTo::create(_duration/4, 1.0f);
    ActionInterval *scaleOut = ScaleTo::create(_duration/4, 0.5f);

4. Create two queues operation.

    auto jumpZoomOut = Sequence::create(scaleOut, jump, nullptr);
    auto jumpZoomIn = Sequence::create(jump, scaleIn, nullptr);

The two scenarios runAction.

    ActionInterval *delay = DelayTime::create(_duration/2);

    _outScene->runAction(jumpZoomOut);
    _inScene->runAction
    (
        Sequence::create
        (
            delay,
            jumpZoomIn,
            CallFunc::create(CC_CALLBACK_0(TransitionScene::finish,this)),
            nullptr
        )
    );

Before the end of the sequence of actions to enter the scene of the execution of the parent class TransitionScene :: finish.

- TransitionScene finish

Briefly, a set of attributes inScene and outScene. Thus for screen window, effects of the implementation of the transition is completed, inScene visible, outScene not visible, the position of two scenes, rotate, zoom are set to default values.

After the property is set, execute:

    this->schedule(CC_SCHEDULE_SELECTOR(TransitionScene::setNewScene), 0);

The statement built a Timer, the time interval of 0, indicating that the next frame is necessary to perform the callback function parameters setNewScene.

- TransitionScene setNewScene

1. First Timer unschedule the previous frame.

2. The director attribute _sendCleanupToScene assigned TransitionScene property _isSendCleanupToScene.

3. The Executive Director of replaceScene (_inScene), the inScene as a nextScene director.

4. Finally outScene visible.

7. The process of running a scene change

Here, for example with replaceScene.

Example, we click on the menu item, triggering replaceScene function, parameter is a transition scene.

Transition scene began to onEnter ()

GLView 1. This frame is received touch event (mouse click on the menu item), triggering a callback function, the callback function created a new scene and scene transitions, inScene transition scene is replaceScene executive director of the parameters for the new scene.

2. replaceScene in: home director _sendCleanupToScene true, the new scene stack, new scenes become nextScene. nextScene had to NULL.

3. The end of the frame. Go to the next frame.

4. After the next frame is determined to exist scheduler update nextScene, performed setNextScene method.

5. setNextScene:

void Director::setNextScene()
{
    _eventDispatcher->dispatchEvent(_beforeSetNextScene);

    bool runningIsTransition = dynamic_cast<TransitionScene*>(_runningScene) != nullptr; // false
    bool newIsTransition = dynamic_cast<TransitionScene*>(_nextScene) != nullptr; // true

     if (! newIsTransition) //新场景是过渡场景,不执行
     {
         if (_runningScene)
         {
             _runningScene->onExitTransitionDidStart();
             _runningScene->onExit();
         }
 
         // issue #709. the root node (scene) should receive the cleanup message too
         // otherwise it might be leaked.
         if (_sendCleanupToScene && _runningScene)
         {
             _runningScene->cleanup();
         }
     }

    if (_runningScene)
    {
        _runningScene->release();
    }
    _runningScene = _nextScene;
    _nextScene->retain();
    _nextScene = nullptr; //nextScene至此成为了runningScene

    if ((! runningIsTransition) && _runningScene) // once runningscene is false, execution 
    { 
        _runningScene -> the onEnter (); // the onEnter scene transition 
        _runningScene-> onEnterTransitionDidFinish (); 
    } 
    
    _eventDispatcher -> the dispatchEvent (_afterSetNextScene); 
}

6. Do onEnter transition scenario:

  1. Perform Node onEnter.

  2. Perform TransitionScene onEnter, director of eventDispatcher disabled, to withdraw from the scene onExitTransitionDidStart, to enter the scene onEnter.

  3. Create action sequences inScene and outScene respectively. Respectively runAction.

End of the transition scene to onExit ()

1. scene transition, the last action scene of a motion sequence is a new callback operation CallFunc, call TransitionScene finish (), after setting properties, in the next frame by calling setNewScene Scheduler method.

2. setNewScene in: replaceScene (inScene), to achieve a nextScene point to the new scene. At this point, runningScene transition scene.

3. The next frame will be the next executive director setNextScene, because runningScene and nextScene there, runningScene transition scenario execution onExitTransitionDidStart onExit.

bool runningIsTransition = dynamic_cast<TransitionScene*>(_runningScene) != nullptr; // true
    bool newIsTransition = dynamic_cast<TransitionScene*>(_nextScene) != nullptr; //false

     if (! newIsTransition) //执行
     {
         if (_runningScene) //过渡场景onExit
         {
             _runningScene->onExitTransitionDidStart();
             _runningScene->onExit();
         }
 
         if (_sendCleanupToScene && _runningScene)
         {
             _runningScene->cleanup();
         }
     }
 //......

onExit transition scene, to withdraw from the scene execution onExit, completely out of the scene to appear execute onEnterTransitionDidFinish, full access. Meanwhile, the director of eventDispatcher enabled.

cleanup()

Continue setNextScene, _sendCleanupToScene set true when replaceScene, transition scenario execution TransitionScene cleanup ().

    Scene::cleanup();
    if( _isSendCleanupToScene )
        _outScene->cleanup();

Called Node cleanup (). _isSendCleanupToScene set by the director _sendCleanupToScene, it is true, that you want to exit the scene cleanup ().

_sendCleanupToScene

When set to true director replaceScene, director pushScene set to false, director popScene set true.

It is true, when the director setNextScene, runningScene cleanup (the director), and then is nextScene runningScene alternative.

If the transition at this time runnningScene scene, cleanup (transition scene) in addition to their own cleanup, but also whether or not you want to exit scene cleanup by _isSendCleanupToScene judgment.

_isSendCleanupToScene is at the end of the transition action scenes, director _sendCleanupToScene direct assignment.

So, when the director with a scene transition as replaceScene parameters, the transition to withdraw from the scene and a scene will be cleanup.

Guess you like

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