Director, scenario, layer, genie

Source: Internet
Author: User

Director, scenario, layer, genie
Basic concepts-directors, scenarios, layers, and genie

In the Cocos2d-x-3.x engine, the use of node tree structure to manage game objects, a game can be divided into different scenes, a scene can be divided into different layers, A layer can have any visible game Node (that is, an object. Basically, all classes in the game are derived from Node nodes ). You can execute actions to modify the attributes of a game node to move, rotate, zoom in, or zoom out the node.

Each time a scenario runs independently, a game process is completed by switching between different scenarios. The management of the game process is implemented by ctor. The basic framework class diagram is as follows:

Director)

A game is like a movie, but the game is more interactive, but their basic principles are consistent. Therefore, Cocos2dx abstracts the overall game situation category into a Director. Director is the core of the entire cocos2dx engine and the navigator 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, control of nodes (game elements), storage and calling of game data, and acquisition of screen sizes must be managed and controlled by the ctor class.

Because Director is the chief Director of the game project and often calls for some control, the director uses a single-piece design mode, that is, the Director obtained in the project is the same. You can use the getInstance () method to obtain the Director instance. For more information about the API, see the relevant documentation.

Scenario (Scene)

Scene scenarios are also an essential element of cocos2dx. In games, we usually need to build different scenarios (at least one). In the game, switching between levels and sections is also a scenario switch, just like changing the stage and venue in a movie. A major role of a scenario is process control. We can use Director's series of methods to control the free switching of different scenarios in the game.

The following are common Director control methods:

  • RunWithScene (Scene * scene) starts the game and runs the scene scenario. This method is called when the main program starts the main scenario for the first time. This method cannot be called if a running scenario already exists. pushScene --> startAnimation is called.
  • PushScene (Scene * scene) pause the currently running scenario and press it into the code execution scenario stack. Then, it sets the passed-in scene to the current running scenario, this method is called only when a running scenario exists;
  • ReplaceScene (Scene * scene) directly replaces the current scene with the input Scene to switch the screen. The current scene is released. This is the most common method for switching scenarios.
  • PopScene () releases the current scenario, then pops up the stack top scenario from the code execution scenario, and sets it to the current running scenario. If the stack is empty, terminate the application directly. Pair with PushScene
  • End () releases and terminates the execution scenario, and exits the application at the same time.
  • Pause () pauses all the timers and actions in the current running scenario, and the scenario will still be displayed on the screen.
  • Resume () restores all timers and actions in the current running scenario, and the scenario is still displayed on the screen.

    At the same time, the scenario is a layer container that contains all game elements to be displayed. Generally, 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 of sub-classes, add layers for scenarios, and start music playing.

    Layer)

    Layer is a Node subclass that processes player event responses. Different from the scenario, a layer usually contains content directly displayed on the screen and can accept user input events, including touch, accelerometer, and keyboard input. We need to add genie, text tags, or other game elements to the layer, and set the game element attributes, such as location, direction, and size, and set the action of game elements. Normally, the object functions in the layer are similar, and the coupling is tight. The logic Code related to the game content in the layer is also written in the layer. After the layer is organized, you only need to add layers to the scene in sequence to display them. To add a layer to a scenario, we can use the addChild method.

    AddChild (Node child) addChild (Node child, int zOrder) addChild (Node * child, int zOrder, int tag)

    The Child parameter is a node. For scenarios, we usually add nodes as layers. The added layer is placed under the added layer. Different zOrder values can be used to specify the order. 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 in its parent node to find it. A Layer can contain any Node as a subnode, including Sprites, Labels, and even other Layer objects.

    There are three different layers in the scenario named HelloWorldScene in the image, and there is a different genie on the layer3 layer.

    The following is an example of creating three different layers:

    auto layer = LayerColor::create(Color4B(0, 128, 128, 255));layer->setContentSize(CCSizeMake(120, 80));layer->setPosition(Point(50, 50));addChild(layer, 10);auto layer1 = LayerColor::create(Color4B(128, 0, 128, 255));layer1->setContentSize(CCSizeMake(120, 80));layer1->setPosition(Point(100, 80));addChild(layer1, 20);auto layer2 = LayerColor::create(Color4B(128, 128, 0, 255));layer2->setContentSize(CCSizeMake(120, 80));layer2->setPosition(Point(150, 110));addChild(layer2, 30);

    Genie

    Cocos2d's genie are similar to those in other game engines. They can be moved, rotated, scaled, animated, and converted. The Sprite of Cocos2dx is composed of Texure, frame, and animation, and is rendered by openes. A simple process can be described as follows: To load images using Texture2D, you can use Texture2D to generate the corresponding SpriteFrame (SpriteFrame) and add SpriteFrame to Animation to generate Animation data, use Animation to generate an Animation (that is, the final Animation action), and use Sprite to execute this action.

    Several ways to create an genie:

    • Directly create:
          auto sprite = Sprite::create("HelloWorld.png");          this->addChild(sprite,0);
      • Use textures to create genie
            auto sprite1 = Sprite::createWithTexture(TextureCache::getInstance()->addImage("HelloWorld.png"));    this->addChild(sprite1, 0);
        • Use the genie frame to create the genie
              auto sprite2=Sprite::createWithSpriteFrameName("HelloWorld.png");      this->addChild(sprite2, 0);

          The basic process of implementing the wizard display in Cocos2dx is as follows:

          // Create Sceneauto scene = Scene: create (); // create layer auto layer = HelloWorld: create (); // Add the layer to the scenario scene-> addChild (layer); // create an sprite auto Sprite = sprite: create ("HelloWorld.png "); // Add the genie to 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.