Cocos2d-x v3.1 core class ctor, Scene, Layer and Sprite (6)
Scene is like a stage where various elements are placed. Some elements are fixed, such as set sets, and the items are fixed, but some elements can also be moved, such as male and female. This is also true in the gaming world. In a game scenario, we place different elements to embellish our scenario. Of course, in the gaming world, it cannot be completely the same as in the real world. To facilitate element control, we place different elements on different layers, which facilitates management. In a scenario, we can place multiple layers. We can use addChild to add layers to the scene. When designing a scenario, we generally derive a class from the scenario, and then add our layers to it.
Director is a singleton. Only one instance exists in the game. Director Chinese means "Director", in the Cocos2d-x also as its name, it manages scheduling of all scenarios in the Cocos2d-x. It can switch our current scenario (there is only one current scenario in the game ), pause actions in a scenario. This is the same as calling "pause" by a real director. It will pause all actions or sound effects in the entire scenario. Of course, you can also start actions and sound effects in a scenario. All in all, ctor plays the role of a Director.
Layer is the main component element in the scenario. As a result, we can better manage the elements in the scenario. The concept of layers here is the same as that of Adobe Photoshop. When we design the elements on the Layer, a class is generally derived from the Layer to manage the elements on the Layer. Of course, in addition to providing me with a default transparent layer in the Cocos2d-x also provides us with some built-in layers such as Menu, LayerColor and LayerGradient.
Sprite is the main component element in the layer. Since Sprite is inherited from Node, other Sprite can be added as sub-objects to the Sprite object. Sprite is similar to other 2D engines. It is a 2D image that can be rotated, translated, and scaled.
Next we will add three layers of LayerColor, LayerGradient and LayerMultiplex in the scenario based on the preceding Hello world Program. The Code is as follows:
1: Scene * HelloWorld: createScene () 2: {3: // 'Scene 'is an autorelease object 4: auto scene = scene: create (); 5: 6: // 'player' is an autorelease object 7: auto layer = HelloWorld: create (); 8: 9: // add layer as a child to scene 10: scene-> addChild (layer); 11: auto gradientLayer = LayerGradient: create (Color4B (255, 0,255,255), Color4B (0, 0,255,255); 12: gradientLayer-> setContentSize (Size (200,200); 13: gradientLayer-> setPosition (100,100); 14: scene-> addChild (gradientLayer); 15: 16: auto colorLayer = LayerColor:: create (Color4B (255,255, 0,128); 17: colorLayer-> setContentSize (Size (200,200); 18: colorLayer-> setPosition (100 + 250,100); 19: scene-> addChild (colorLayer); 20: 21: // test LayerMultiplex 22: auto gradientLayer1 = LayerGradient: create (Color4B (255, 0,255,255), Color4B (0, 0,255,255); 23: gradientLayer1-> setContentSize (Size (200,200); 24: 25: auto colorLayer1 = LayerColor: create (Color4B (255,128, 0,255); 26: colorLayer1-> setContentSize (Size (200,200); 27: 28: auto multLayer = LayerMultiplex: create (); 29: multLayer-> addLayer (gradientLayer1); 30: multLayer-& gt; addLayer (colorLayer1); 31: multLayer-& gt; switchTo (1); 32: multLayer-& gt; setContentSize (Size (200,200); 33: multLayer-> setPosition (100 + 250 + 250,100); 34: scene-> addChild (multLayer); 35: 36: // return the scene 37: return scene; 38 :}
The effect is as follows: