Cocos2dx Concept 1) scene, inherited from the CCScene scenario. It is a display interface during game operation. One application can have multiple scenes, but only one of them can be activated at a time, it can also be understood that only one interface can be displayed at a time. For example, your game has the following interface: introduction, menu, level scenario, victory scenario, failure scenario, and high score page. So many interfaces constitute the entire game, as shown in a scenario flowchart. A CCScene is composed of one or more ccnodes. You can add ccnodes to scene. Subclasses of CCNode, such as CCLayer and CCSprite, can provide the appearance and behavior of the scenario. Because the scenario is a subclass of CCNode, you can use CCActions to make some animations. 2) ccctor ccdireis a controller that controls switching between different scenarios. It is a singleton. It knows which scenario is active and allows you to change the scenario by replacing the current scenario or putting a new scenario into the scenario stack. When you place a new scenario in the scenario stack, CCDirector suspends the previous scenario, saves it in the memory, and then displays the new scenario. After a while, if you pop the scene at the top of the scene stack, the previous scene will be restored to its final state and displayed. CCDirector is also responsible for initializing OpenGL ES. 3) Layers CCLayer is a CCNode that knows how to handle contact operations. Layers knows how to draw and may be semi-transparent. Players can see things after these Layers. CCLayers is very important in defining game interfaces and actions. during development, you need to spend most of your time writing some CCLayer subclasses to achieve what you want. CCLayer can define the touch callback and inherit some methods to handle the touch event (ccTouchBegan, ccTouchMoved, ccTouchEnded or ccTouchCancelled ). A CClayer can interact with users. These touch events can be passed through all layers in a scenario until a Layer gets them and can process the events. Complex applications require you to customize some CCLayer subclasses. cocos2d provides several predefined layers. For example, CCMenu (a simple menu layer), CCColorLayer (a layer that can draw entity colors), and CCLayerGradient (a layer that can draw gradient colors ), CCLayerMultiplex (a Layer that can process multiple complex child Layers, can activate one child Layer at a time, and disable other Layers at a time) Layers may contain CCNode as child Layers, including CCSprites, CCLabels, and other CCLayer objects. Examples of multiple layers are as follows:
1 CCLayerGradient* layer1 = CCLayerGradient::create(ccc4(255, 0, 0, 255), ccc4(255, 0, 255, 255)); 2 layer1->setContentSize(CCSizeMake(80, 80)); 3 layer1->setPosition(ccp(50,50)); 4 addChild(layer1); 5 6 CCLayerGradient* layer2 = CCLayerGradient::create(ccc4(0, 0, 0, 127), ccc4(255, 255, 255, 127)); 7 layer2->setContentSize(CCSizeMake(80, 80)); 8 layer2->setPosition(ccp(100,90)); 9 addChild(layer2); 10 11 CCLayerGradient* layer3 = CCLayerGradient::create(); 12 layer3->setContentSize(CCSizeMake(80, 80)); 13 layer3->setPosition(ccp(150,140)); 14 layer3->setStartColor(ccc3(255, 0, 0)); 15 layer3->setEndColor(ccc3(255, 0, 255)); 16 layer3->setStartOpacity(255); 17 layer3->setEndOpacity(255); 18 ccBlendFunc blend; 19 blend.src = GL_SRC_ALPHA; 20 blend.dst = GL_ONE_MINUS_SRC_ALPHA; 21 layer3->setBlendFunc(blend); 22 addChild(layer3);
4) Sprites A cocos2d CCSprite is a 2D image element that can be moved, rotated, scaled, and animated. A CCSprite can have other CCSprite as sub-elements. When a parent node is converted, all its sub-elements are converted accordingly.