Heavy developer labor results, reproduced when please be sure to indicate the source: http://blog.csdn.net/haomengzhu/article/details/30474393
Scene: After learning about Director, we will introduce Scene, a game component closely related to it. Through previous learning, we have learned about the scenario and its position in Process Control.
In Cocos2d-x, Scene defines a scenario. The scenario is only a layer container that contains all game elements to be displayed. Therefore, compared with other game elements, Scene does not provide any special functions and is a very simple class. Besides serving as a layer container, another function of a scenario is process control. Using Director: replaceScene and other methods, we can make the game switch freely in different scenarios.
During Game Development, when we need to complete a scenario, we will create a subclass of Scene and implement the functions we need in the subclass. For example, we can load game Resources in the initialization method of sub-classes, add layers for scenarios, and start music playing. At the same time, the scenario may also require a certain transitional effect. Otherwise, the scenario switching will be very abrupt. Cocos2d-x provides a lot of gorgeous switching effects, such as turning pages, waves, fade-in and so on. These effects are implemented through the TransitionScene special effect class derived from Scene. TransitionScene. This scene is a scene used for intermediate transition changes during two scene switches. The interface design is very clear and simple. Header file in cocos \ 2d \ CCTransition. h; of course, you can also imitate the Cocos2d-x built-in scenario switching special effects code, to write your own special effects.
Layer: Layer defines a Layer. Similar to Scene, a layer also plays the role of a container. However, unlike the scenario, a layer usually contains specific content that is directly displayed on the screen: we need to place genie, text tags, or other game elements in the layer; set the game element attributes, such as location, direction, and size, and set the action of the game element.
It can be seen that most of the coding time of game development is used on the Creation layer. In general, the object functions in the layer are similar and tightly coupled, and the logic Code related to the game content in the layer is also written in the layer.
After the layers are organized, you only need to add the layers to the scene in order to display them. To add layers to a scenario, we can use the addChild method.
The addChild method has three definitions:
void addChild(Node* child); //Adds a child to the container with z-order as 0.void addChild(Node* child, int localZOrder);void addChild(Node* child, int localZOrder, int tag);
The child parameter is the node to be added. For scenarios, we usually add nodes as layers. The added layer is placed under the added layer. If you want to specify the order for them, you can use different localZOrder values. localZOrder represents the order of the elements under the node. A larger value indicates a higher display order. The default value of localZOrder is 0. A tag is the identification number of an element. If you set a tag value for a child node, you can use the tag value to locate it in its parent node. Here we can select the method we need to add layers to the scenario.
In the composition of the search for three game scenario mentioned in the previous article, the search for three game scenarios are roughly composed of the backgroundLayer, actionLayer, and touchLayer) and menuLayer. Assuming that these layers have been completed, the last thing we need to do is to add them to the scene in the game scenario initialization method:
this->addChild(backgroundLayer, 0);this->addChild(actionLayer, 100);this->addChild(touchLayer,200);this->addChild(menuLayer, 300);
Another important feature of Layer is to accept user input events, including touch, accelerometer, and keyboard input. In versions earlier than cocos2d-x 3.0, the members of the layer related to user input events are shown in the table.
In version 3.0, the event distribution mechanism is used: (1) an event listener is created first, which contains the following types:
EventListenerTouch)
EventListenerKeyboard)
EventListenerAcceleration)
EventListenerMouse)
Custom Event (EventListenerCustom)
(2) The above event listeners are managed by _ eventDispatcher. It consists of three parts:
EventDispatcher
Event types such as EventTouch and EventKeyboard
Event Listeners such as EventListenerTouch and EventListenerKeyboard
(3) The Listener implements various trigger logics. When appropriate, the event distributor distributes event types and then calls corresponding types of listeners.
Haomeng's main friendly reminder: After learning all the game components, you can build a modular model to complete a game ,,,