Basic concept Introduction--director, scene, layer, Sprite
In the cocos2d-x-3.x engine, the node tree structure to manage the game object, a game can be divided into different scenes, a scene can be divided into different layers, a layer can also have any visible game nodes (that is, the game is basically all the classes are sent to node nodes). You can perform an action to modify the properties of a game node so that it moves, rotates, zooms in, shrinks, and so on.
Each time a scene is running independently, by switching between different scenes to complete a game flow, game process management by the Director to execute, its basic framework class diagram is as follows:
Directing (director)
A game is like a movie, but the game is more interactive, but the basic principle is the same. So in the cocos2dx of the overall game of the general situation of the class abstract as director (director), Director is the core of the entire COCOS2DX engine, is the entire game navigator, some of the common operations in the game is controlled by the director, such as OpenGL The initialization of ES, the conversion of scenes, the control of the game pause, the switch between world coordinates and GL coordinates, the control of nodes (game elements), and some game data saving calls, screen size acquisition, etc. must be managed and controlled by the Director class.
Because Director is the general director of a game project, it is often called to do some control, so the director takes advantage of the single-piece design pattern, which is the same director that is taken from the project. Use the getinstance () method to obtain the director's example, the specific API can refer to the relevant documents, do not repeat.
Scenes (Scene)
Scene scenes are also essential elements in the COCOS2DX, in which we often need to build different scenes (at least one), and the game's levels and sections are switched in a single scene, just like changing the stage and venue in a movie. One of the important functions of the scene is the role of process control, and we can control the free switching of different scenes in the game through a series of director's methods.
The following are common methods for director control scenarios:
- Runwithscene (Scene *scene) launches the game and runs scene scenes. This method is called when the main program starts the main scene for the first time. The method cannot be called if there is already a running scene; Pushscene-->startanimation is called.
- Pushscene (scene *scene) pauses and presses the currently running scene into the code execution scenario stack, then sets the incoming scene to the current running scene, and calls the method only if there is a running scene;
- Replacescene (Scene *scene) uses the incoming scene to switch the screen directly, replacing the current scene with the current scenario being released. This is the most common way to switch scenes.
- Popscene () releases the current scene and then pops the top of the stack from the code execution scene and sets it as the current running scene. If the stack is empty, end the app directly. and Pushscene knot pair use
- End () Releases and terminates the execution scenario while exiting the application
- Pause () Pauses all timers and actions in the current running scene, and the scene still appears on the screen
- Resume () Restores all timers and actions for the current running scene, and the scene still appears on the screen
The scene is also a layer container that contains all the game elements that need to be displayed. Typically, when we need to complete a scene, we create a subclass of the scene and implement the functionality we need in the subclass. For example, we can load game resources in the initialization of subclasses, add layers to scenes, start music playback, and so on.
Layers (layer)
A layer is a node subclass that handles the response of a player event. Unlike scenarios, layers typically contain content that is rendered directly on the screen and can accept user input events, including touch, accelerometer, and keyboard input. We need to add sprites, text tags or other game elements to the layer and set the attributes of the game elements, such as position, direction and size, set the action of the game elements, etc. Usually, the objects in the layer function Similarly, the coupling is tighter, and the logic code associated with the game content in the layer is written in the layer, and after organizing the layers, you simply add the layers to the scene in order to display them. To add layers to a scene, we can use the Addchild method.
AddChild (Node Child) addChild (node child, int zOrder) addChild (node *child, int zOrder, int tag)
Where the child parameter is the node. For scenarios, the nodes we add are usually layers. The layer that is added first is placed below the layer that is added later. If you need to prioritize them, you can use different zorder values. tag is the identification number of the element, and if the tag value is set for the child node, it can be found by using the tag value in its parent node. Layers can contain any node as child nodes, including sprites (sprites), Labels (tags), and even other layer objects.
In the picture shown, there are three different layers in the scene called Helloworldscene, and there is a different sprite on the LAYER3 layer.
Below is an example of creating three different layers:
auto layer = layercolor::create (color4b (0, +, N, 255)) Layer->setcontentsize (CCSizeMake ( (layer->setposition); AddChild (layer, ten), Auto layer1 = Layercolor::create (color4b (128, 0, (255)), Layer1->setcontentsize (Ccsizemake), layer1->setposition (point), AddChild (Layer1, Auto Layer2 = Layercolor::create (color4b (80, 0, 255)), Layer2->setcontentsize (Ccsizemake (120,)); Layer2->setposition (Point (), AddChild (layer2);
Elves
The elves in cocos2d are similar to the sprites in other game engines, which can move, rotate, scale, animate, and accept other transformations. The COCOS2DX sprite is made up of texure,frame and animation and is rendered by Openes. The simple process can be described as: using texture2d loading pictures, you can use texture2d to generate corresponding Spriteframe (Sprite frame), add spriteframe to animation to generate animation data, Use animation to generate animate (which is the final animation action) and then perform this action with a sprite.
Several ways to create sprites:
auto sprite = Sprite::create("HelloWorld.png"); this->addChild(sprite,0);
- Use textures to create sprites
auto sprite1 = Sprite::createWithTexture(TextureCache::getInstance()->addImage("HelloWorld.png")); this->addChild(sprite1, 0);
- Use sprite frames to create sprites
auto sprite2=Sprite::createWithSpriteFrameName("HelloWorld.png"); this->addChild(sprite2, 0);
The basic process for implementing sprite display in COCOS2DX is as follows:
//创建Sceneauto scene = Scene::create();//创建层auto layer = HelloWorld::create();//把层加入场景中scene->addChild(layer);//创建一个精灵auto sprite = Sprite::create("HelloWorld.png");//把精灵加到层里layer->addChild(sprite, 0);
Director, scene, layer, Sprite