Basic concepts of cocos2d-x-3.6 engine, cocos2d-x-3.6 Engine

Source: Internet
Author: User

Basic concepts of cocos2d-x-3.6 engine, cocos2d-x-3.6 Engine

First, let's talk about several important basic concepts in the engine: Directors, nodes, scenarios, layers, and genie. Of course, developers will encounter many other concepts, but it doesn't matter. With these basic concepts, it will be much easier to learn later.

  • Node is the most basic class in the cocos2d-x. Almost all classes in the game are derived from this class. In other words, almost all game objects in the game are a Node. So how to manage these game objects, the cocos2d-x engine uses the node tree to manage all the game objects. Let's look at the relationship between each element in a game. This is not a class chart, but an Organizational Relationship Chart.

    A game is composed of many game scenarios. It is like a movie. Each story is played in a specific scenario and is always played in a single venue. More than N game scenarios form a game, and the Director is responsible for organizing these scenarios, just like the Director in a movie. As shown in the figure, there will be many layers in the scenario, and there will be a lot of Sprite in the Layer ). This can be understood as a movie scenario, such as an external scene, with a large background in the distance, and then a close view and environment light, which can be expressed in many layers in the game, the foreground uses a layer, a near-landscape layer, and an environment light layer. These layers are displayed on the screen in a certain order. There are a lot of genie in the layer, that is, the content in each layer is represented by one Genie. If there is a row of houses and a lot of cars are in the shuttle, this row of houses is an genie, and each car is an genie. There is a dog wandering in the near view. The dog is an genie, and the environment light is a moon, and the moon is an genie. Scenario, layer, and genie are essentially a node. Because they are derived from node classes, they are a node tree structure.

  • The Director is a single-column class. There is only one instance in the entire game lifecycle, and you do not need to manually create instances. The engine has generated an instance of the entire Director class during initialization. The director is the core of the entire game, just like the director of the film, controlling the scheduling of the entire game. Some common operations in the game are controlled by Director, such as OpenGL ES initialization, scene conversion, continuous control of the game pause, switching between world coordinates and GL coordinates, the control of nodes (game elements), the storage and calling of game data, and the acquisition of screen sizes.

  • Scene (Scene) is a one-screen Scene in the game. All the stories in the game are in the Scene. The unit of a game is scene. All running game elements are placed in the game scene. Let's see how the simplest helloworld is used. Create a scenario directly and the director will execute the scenario.

    // create a scene. it's an autorelease object    auto scene = HelloWorld::createScene();    // run    director->runWithScene(scene);

Let's look at the implementation of the createScene function.

Scene* HelloWorld::createScene(){    // 'scene' is an autorelease object    auto scene = Scene::create();    // 'layer' is an autorelease object    auto layer = HelloWorld::create();    // add layer as a child to scene    scene->addChild(layer);    // return the scene    return scene;}

First, create a Scene, and then create a Layer (HelloWorld). Note that HelloWorld is derived from the Layer, so it is a Layer, and then add this to the Scene (Scene, return (Scene ). This is the simplest HelloWorld scenario. When the director executes this game scenario, a HelloWorld page is displayed.

  • Layer and Sprite ). Game operation is a scenario, but each scenario uses layers to organize game content. For example, in HelloWorld scenarios, layers must be added to the scenario, layers are fragmented game elements. In a layer, basically all game elements are genie, such as the background, an genie generated using a background image, a character, a character genie generated using a character image, a car, cats and dogs are all genie. Sprite is the most common game unit, not just a character.

Now let's sum up, cocos2dx manages the tree structure of Game objects. Scenarios, layers, and genie are all derived classes of nodes, so they are actually all nodes. The simplest structure of a game is this: Create a scenario (this is a node), add a layer (and a node) to the scenario ), the layer is added with an genie (and a node). This layer is a node tree. The director executes the scenario and manages the entire node tree. Over.

Related Article

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.