"Cocos2d Introductory Tutorial II" Cocos2d-x basic article

Source: Internet
Author: User
Tags addchild

The previous chapter has studied the construction of the environment. This chapter is a master of the basic concepts. The content is about:

1. Director

2. Scene

3. Node

4. Layer

4. Elves

1. Directing (director)

The main role of the director exists:

Environment settings (frame rate initialization OpenGL and renderer)

Scene Management

Perform the main loop

The game is a dead cycle of continuous rendering just like the video frame of a frame of the drawing out to say a frame of 1/60 seconds that is cocos2d a second render 60 times

Director's management: Timer Event manager Action Manager

Director inherits from ref a singleton class

Get Director Class Director instance statement

Auto Director = Director:: getinstance ();

1 //call the first scene, there is currently no scene to run2 voidRunwithscene (Ccscene *pscene); 3   4 //scene into the stack, there must be a scene now5 voidPushscene (Ccscene *pscene); 6   7 //scene out of the stack, delete the current scene, there must be a scene now8 voidPopscene (void); 9   Ten //eject all scenes until the root scene One voidPoptorootscene (void);  A    - //popup to specified scene - voidPoptoscenestacklevel (intLevel );  the    - //replace the old scene with a new scene - voidReplacescene (Ccscene *pscene);  -    + //End Run, release the scene, must be called manually - voidEndvoid);  +    A //Pause Scene at voidPausevoid);

2. Scenario (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.

3.Node Node

The core class instance of Cocos2d-x is a node, which is inherited from a tree structure whose base class is node.

Node main feature, which can contain other inherited nodes such as layer, Sprite, etc., you can set a periodic callback function, you can perform the action.

node inherits from the ref class.

4. Layers (layer)

Layer is a subclass of node nodes. Layers 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.

1 AddChild (Node child) 2 int  3intint 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.

5. Sprites (Sprite)

several ways to create sprites:

    • created directly:
       1  auto Sprite = Sprite:: Create ( helloworld.png   " );  2  this ->addchild (sprite,< Span style= "color: #800080;"
      >0 );  
    • use textures to create sprites
       1  Auto sprite1 = Sprite::createwithtexture (Texturecache::getinstance () >addimage ( " helloworld.png    2  0 );  
    • use sprite frames to create sprites
       1  auto Sprite2=sprite :: Createwithspriteframename ( helloworld.png   );  2  0 );  

Wizard-Created process:

Create a Sceneauto scene = Scene::create ();//create layer Auto layer = helloworld::create ();//Add layer to the scene scene->addchild (layer);// Create a Sprite Auto Sprite = sprite::create ("Helloworld.png");//Add sprites to the layer Layer->addchild (sprite, 0);

The elf creates the underlying source code (this part can be ignored if you can't read it):

Look for this image from the sprite frame, and if you can't find it, the compiler will drop it directly.

The following is the basic architecture diagram of the four-person relationship:

It's normal to start feeling a little bit out of the ordinary, first of all know their main is what the line. Then slowly will understand.

"Cocos2d Introductory Tutorial II" Cocos2d-x basic 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.