Analysis on Switching Principle of Cocos2d-x learning notes (8)

Source: Internet
Author: User

Analysis on Switching Principle of Cocos2d-x learning notes (8)

 

 

Preface

The implementation principle of CCAction is finally finished. Now, there is a theoretical basis for the principle of scenario switching. In fact, it is not difficult to guess that many scenario switching effects are achieved through CCAction.

 

CCDirector::sharedDirector()->replaceScene(CCTransitionRotoZoom::create(2.0f, MenuScene::scene()));
This is a basic scenario switch code. Let's start with replaceScene ~

 

 

CCDirector: replaceScene

 

Void CCDirector: replaceScene (CCScene * pScene) {CCAssert (m_pRunningScene, Use runWithScene: instead to start the director); // you can determine whether a running scenario exists, replaceScene CCAssert (pScene! = NULL, the scene shoshould not be null); // make sure that the unsigned int index = m_pobScenesStack-> count () is not empty in the scenario to be switched (); // obtain the number of scenarios in the scenario stack m_bSendCleanupToScene = true; // mark the empty scenario as true m_pobScenesStack-> replaceObjectAtIndex (index-1, pScene ); // place the scenario at the end of the container m_pNextScene = pScene; // set the next scenario to pScene}
When I saw this, I felt that I could not go down. I finally found the result. The answer is mainLoop in the main loop. We will call the drawScene method of the device.

 

CCDirector: drawScene
Void CCDirector: drawScene (void) {calculateDeltaTime (); // calculate the frame interval if (! M_bPaused) // ctor {m_pScheduler-> update (m_fDeltaTime); // update destroy function manager} glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // OPENGL clears the color and depth of the background buffer. This is the processing before each frame of OPENGL is rendered. If (m_pNextScene) {setNextScene (); // if a new scenario needs to be set, call the setNextScene function for related processing. }...}

It is not difficult to find that the key should be in the setNextScene method.

 

CCDirector: setNextScene

 

Void CCDirector: setNextScene (void) {bool runningIsTransition = dynamic_cast
 
  
(M_pRunningScene )! = NULL; // defines the bool variable runningIsTransition to determine whether the current running scenario is a switching scenario class. If yes, set runningIsTransition to true bool newIsTransition = dynamic_cast
  
   
(M_pNextScene )! = NULL; // defines the bool variable newIsTransition to determine whether the new scenario to be set is a switching scenario class. if yes, set newIsTransition to true if (! NewIsTransition) {// if the new scenario is not a switching scenario class but a common scenario if (m_pRunningScene) {// if m_pRunningScene is not empty, then the method m_pRunningScene-> onExitTransitionDidStart (); m_pRunningScene-> onExit ();} // if m_bSendCleanupToScene is marked as true and a running scenario exists, call its cleanup function if (Response & m_pRunningScene) {m_pRunningScene-> cleanup () ;}} if (m_pRunningScene) {// call the release function m_pRunningScene-> release ();} m_pRunningScen for the currently running scenario E = m_pNextScene; // set the new scene to m_pNextScene-> retain (); // Add 1 m_pNextScene = NULL to count the new scene; // set the pointer of the new scenario to null if ((! RunningIsTransition) & m_pRunningScene) {// call the onEnter function m_pRunningScene-> onEnter (); m_pRunningScene-> onEnterTransitionDidFinish ();}}
  
 

 

CCTransitionRotoZoom: onEnter

At this point, we have probably learned about the scenario switching steps. The final implementation of the switching action is still based on the implementation of the scenario switching class itself.

 

Void CCTransitionRotoZoom: onEnter () {// call the corresponding function CCTransitionScene: onEnter () of the base class; // set the initial scaling value of the new scenario to 0.001, reduced by 100 times m_pInScene-> setScale (0.001f); // set the initial scaling value of the original scene to the original size m_pOutScene-> setScale (1.0f ); // set the scene's anchorpoint as the rotation and scaling center m_pInScene-> setAnchorPoint (ccp (0.5f, 0.5f); m_pOutScene-> setAnchorPoint (ccp (0.5f, 0.5f )); // create a sequence animation CCActionInterval * rotozoom = (CCActionInterval *) (CCSequence: create (CCSpawn: create // This sequence animation has two parts. The first part is a combination animation, this composite animation is composed of a combination of scaling and rotation (CCScaleBy: create (m_fDuration/2, 0.001f). // within half the animation time, It is reduced by 100 times from the current scaling value. CCRotateBy :: create (m_fDuration/2,360*2), // rotate two loops of NULL from the current rotation state in half of the animation time), CCDelayTime: create (m_fDuration/2 ), // The second part of the sequence animation is a NULL pause animation that takes half the animation time); // you can run the sequence animation created above on the original scene, let it rotate 2 circles in half the animation duration, and reduce it by 100 times from the original size, and then pause the animation duration m_pOutScene-> runAction (rotozoom ); // run another sequence animation m_pInScene-> runAction (CCSequence: create (// This sequence animation also consists of two parts, the first part is the reverse animation of the above sequence animation, the second part is to call the finish function rotozoom-> reverse (), CCCallFunc: create (this, callfunc_selector (CCTransitionScene: finish )), NULL ));}

Conclusion

As we can see, our initial idea is basically proved, but it is a bit different from the tone. After replaceScene, the scenario does not switch directly, but waits until the main thread step continues, in the past two days, I have briefly explained the principles of Action and scenario switching. Are you more interested in principles?

 

References

Cocos2d-x 2.0 TestCpp scene switching animation in-depth analysis: http://blog.csdn.net/honghaier/article/details/8475341

 

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.