Cocos2d-director, scene, layer, Elf

Source: Internet
Author: User
Tags addchild

cocos2d-x-3.x engine, the node tree structure to manage the game object, a game can be divided into different scenes, each scene can be divided into different layers, each layer can also have any visible game nodes (that is, the game is basically all the classes are sent to node class nodes), by executing action to modify The properties of the game node so that it moves, rotates, enlarges, and so on.
Each time a scene is running independently, by switching between different scenes to complete a game process, the management of the game process is performed by the Director, the basic Framework class diagram is:
Directing (director)

Director is the core of the entire COCOS2DX engine and is the entire game navigator.
Director as the overall director of the game, will often call some control, so the use of a single-piece design mode, that is, the project inside the channel director is the same, using the getinstance () method to obtain a director instance. scenes (Scene)

An important role of the scene is the role of process control, a common method of 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 a layer container, containing all the game elements that need to be displayed, when a scene is completed, a sub-class of scene is created, and the child class is initialized to load the game resources, add layers to the scene, start the music playback, and so on. layers (layer)

Layer is a node subclass that handles the response of a player's event, unlike the concept of a scene, which typically contains 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 properties of the game elements, such as position, direction, and size, as well as set the action of the game elements.
In general, the objects in the layer function Similarly, the coupling is tighter, and the logic code related to the game content in the layer is written in the layer, after organizing the layer, only need to add the layer in order to the scene can be displayed, you can addchild using the 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.

There are three different layers in the helloworldscene scene, and there is a different sprite on the LAYER3 layer.
An example of creating three different layers:

Auto layer = layercolor::create (color4b (0, +, +, 255));
Layer->setcontentsize (Ccsizemake ());
Layer->setposition (Point ());
AddChild (layer, ten);
Auto Layer1 = layercolor::create (color4b (0, +, 255));
Layer1->setcontentsize (Ccsizemake ());
Layer1->setposition (Point (+));
AddChild (layer1);
Auto Layer2 = layercolor::create (color4b (n, 0, 255));
Layer2->setcontentsize (Ccsizemake ());
Layer2->setposition (Point (a));
AddChild (Layer2, 30);

Sprites (Sprite)

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. directly Create

Auto Sprite = sprite::create ("Helloworld.png");      
    This->addchild (sprite,0);
creating with Textures
Auto sprite1 = Sprite::createwithtexture (Texturecache::getinstance ()->addimage ("Helloworld.png"));
    This->addchild (sprite1, 0);
creating with Sprite frames
Auto Sprite2=sprite::createwithspriteframename ("Helloworld.png");
    this->addchild (sprite2, 0);
Specific Use
Create scene
Auto scene = Scene::create ();
Create layers
Auto layer = Helloworld::create ();
Adding layers to the scene
scene->addchild (layer);
Create a sprite
auto sprite = sprite::create ("Helloworld.png");
Add the elves to the layer
layer->addchild (Sprite, 0);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.