Cocos2d-x Study Notes (4) Analysis of CCScene
Preface
As mentioned above, CCScene is a scenario and a stage for the entire game. by viewing the source code, we can easily find that CCScene is also a child node of CCNode, it also has all the non-private attributes and methods of CCNode described in the previous section.
The purpose of CCScene is to contact all CCNode nodes (including CCLayer and CCSprite ).
When you clearly understand the content to be displayed in each scenario, define different ccnodes and add them to CCScene. Execute CCDirector-> runWithScene () to display them on the screen.
Source code analysis
class CC_DLL CCScene : public CCNode{public: CCScene(); virtual ~CCScene(); bool init(); static CCScene *create(void);};CCScene::CCScene(){ m_bIgnoreAnchorPointForPosition = true; setAnchorPoint(ccp(0.5f, 0.5f));}CCScene::~CCScene(){}bool CCScene::init(){ bool bRet = false; do { CCDirector * pDirector; CC_BREAK_IF( ! (pDirector = CCDirector::sharedDirector()) ); this->setContentSize(pDirector->getWinSize()); // success bRet = true; } while (0); return bRet;}CCScene *CCScene::create(){ CCScene *pRet = new CCScene(); if (pRet && pRet->init()) { pRet->autorelease(); return pRet; } else { CC_SAFE_DELETE(pRet); return NULL; }}
We can find that there are not many codes implemented by the entire CCScene class, mainly because the anchor is set in the central position in the constructor, however, when you set the position, ignore the coordinate of the anchor (we know that the default anchor position of CCNode is (0, 0), that is, the lower left corner of the node ). As a scenario class, why are there no other methods for these two functions, because currently CCScene is responsible for the function of a container, and the objects to be rendered during game development are managed in a unified manner in CCScene, including creation, destruction, and scenario switching.
During scenario switching, you can often see the switching effect. In fact, the system has provided us with a set of effects, all of which are inherited from CCTransitionScene. Let's take a look at its source code:
Class CC_DLL CCTransitionScene: public CCScene {protected: CCScene * m_pInScene; CCScene * m_pOutScene; float m_fDuration; bool break; public: CCTransitionScene (); virtual ~ CCTransitionScene (); virtual void draw (); virtual void onEnter (); virtual void onExit (); virtual void cleanup (); static CCTransitionScene * create (float t, CCScene * scene); // This method initializes the transition effect of a scenario and specifies the transition time and virtual bool initWithDuration (float t, CCScene * scene) of the scenario to be transitioned ); // This method calls void finish (void) at the end of the transition effect; // This method is used to hide void hideOutShowIn (void) in a more external scenario. protected: virtual void sceneOrder (); private: void setNewScene (float dt );};
For example, you can write a simple switching effect for a fade-in or fade-out scenario. The scenario is transitioned to another scenario within 1 s.
CCTransitionFade * transitionScene = CCTransitionFade::initWithDuration(1, gameScene);CCDirector::sharedDirector()->replaceScene(transitionScene);
Result Summary of built-in scenario Switching
// Turbine effect class CC_DLL CCTransitionRotoZoom: public CCTransitionScene // skip to class CC_DLL failed: public CCTransitionScene // switch from left/right/top/bottom to class CC_DLL failed: public CCTransitionScene, public synchronized CC_DLL failed: public synchronized CC_DLL CCTransitionMoveInT: public synchronized CC_DLL failed: public synchronized // slide from left/right/up/down to class CC_DLL failed: public CCTransitionScene, public synchronized CC_DLL CCTransitionSlideInR: public writable class CC_DLL CCTransitionSlideInT: public writable CC_DLL values: public writable // enters class CC_DLL values: public CCTransitionScene, public CCTransitionEaseScene // flipped to class CC_DLL CCTransitionFlipX: public variables // flip up and down to Enter class CC_DLL CCTransitionFlipY: public CCTransitionSceneOriented // flip the upper-right and lower-right axes to class CC_DLL CCTransitionFlipAngular: public variables // zoom in and zoom in to class: public variable // up/down zoom in and out effect turn class CC_DLL into: public variable // up/down left zoom in and out effect turn class CC_DLL into: public CCTransitionSceneOriented // dimmed, then fade into class CC_DLL CCTransitionFade: public CCTransitionScene // Enter class CC_DLL gradient: public CCTransitionScene // small square disappears into class CC_DLL gradient: public CCTransitionScene, public variable // The vertical bar switches to class CC_DLL gradient: public CCTransitionScene, public variable // switch to class CC_DLL variable: public variable // the upper right corner of the cell bar shows the class CC_DLL CCTransitionFadeTR: public CCTransitionScene, and the lower left corner of the cell bar shows the class CC_DLL CCTransitionFadeBL: public CCTransitionFadeTR // Enter class CC_DLL CCTransitionFadeUp: public CCTransitionFadeTR // Enter class CC_DLL CCTransitionFadeDown: public CCTransitionFadeTR
In subsequent notes, we will talk about CCAction. We will also learn the principle of scene switching animation after learning CCAction. Now, let's talk about this in this section. Let's take a look at CCLayer in the next section ~