Introduction to common classes of Cocos2d-x

Source: Internet
Author: User

Cocos2d-x as a game engine across several mainstream platforms, has been a lot of developers love, preliminary contact with the engine, basic knowledge of the engine to do a simple understanding and learning, this article mainly introduces some common classes for this engine.

1. CCNode)

CCNode is the main class in the Cocos2D-x that inherits from CCObject.

Any node class that needs to be displayed on the screen. The most common node classes include:

Scenario (CCScene), set layer (CCLayer), CCSprite, and CCMenu ).

El:

Bool HelloWorld: init ()

{

If (! CCLayer: init ())

{

Return false;

}

// Create a node

CCNode * anode = CCNode: create ();

// Add a node as a subnode to the scenario class

This-> addChild (anode, 0 );

2. CCDirector (directors)

The CCDirector class is the manager of the entire game, including managing the beginning and end of the game.

El:

Bool AppDelegate: applicationDidFinishLaunching (){

// Obtain the director class

CCDirector * pDirector = CCDirector: shareddire ();

// Sets the OpenGL view.

PDirector-> setOpenGLView (& CCEGLView: Export dopenglview ());

// Set whether to display the time of each frame

PDirector-> setDisplayStats (true );

// Set the time of each frame

PDirector-> setAnimationInterval (1.0/60 );

// Create a scenario

CCScene * pScene = HelloWorld: scene ();

// Running scenario

PDirector-> runWithScene (pScene );

Return true;

}

Code List 3-7 Director work when the game enters the background or returns from the background

VoidAppDelegate: applicationDidEnterBackground (){

CCDirector: sharedDirector ()-> stopAnimation ();

}

VoidAppDelegate: applicationWillEnterForeground (){

CCDirector: sharedDirector ()-> startAnimation ();

}

3. Scenario (CCScene)

The CCScene class is a subclass of CCNode. Compared with CCNode, it only adds a feature, that is, it has its own anchor and its position is in the center of the screen. In addition, it does not have any additional functions, but is just an abstract concept.

El:

CCScene * HelloWorld: scene ()

{

// Create a scenario instance

CCScene * scene = CCScene: create ();

// CCScene * scene = CCScene: node ();

// Define the set Layer

HelloWorld * layer = HelloWorld: create ();

// Add the set layer to the scenario

Scene-> addChild (layer );

// Return scenario class

Return scene;

}

4. CCLayer)

CCLayer is a subclass of the CCNode class, and implements touch-screen event proxy.

(TouchEventsDelegate) protocol, can implement the CCNode class function, and can process input, including touch screen and

Acceleration Sensor.

El:

CCScene * HelloWorld: scene ()

{

// Create a scenario instance

CCScene * scene = CCScene: create ();

// Define the set Layer

HelloWorld * layer = HelloWorld: create ();

// Add the set layer to the scenario

Scene-> addChild (layer );

Return false;

}

5. Menu class (CCMenu)

CCMenu is a container for menu items. It is used to load various menu items.

6. control class and its subclass:

(1). the control class CCControlSlider for dragging the slider

(2) color selection disc class CCControlColourPicker

(3) CCControlSwitch

(4) CCControlButton

7. Genie class (CCSprite)

The genie class is represented by a rectangle of a graph or image, which mainly represents the characters in the game.

8. Map class CCTexture2D

Texture fatigue refers to the meaning of an image object. You can use it to create a Sprite object.

9. Genie batch processing class CCSpriteBatchNode

10. spriteframe

The concept of a Sprite frame is relative to an animation. A genie is a fixed section

Point, it can have a lot of spriteframe, switching between them

Formed an animation

11. spriteframecache

The cache class CCSpriteFrameCache is used to store the sprite frames.

Caching is helpful to improve program efficiency. CCSpriteFrameCache

It is a singleton mode. It does not belong to a certain Genie and is shared by all genie.

Of

12. Camera class (CCCamera)

All nodes have a camera class CCCamera. Only through the camera class will the node be rendered

. When a node is scaled or rotated or its position changes, it must overwrite the CCCamera class so that the node passes through

CCCamera Class re-rendering

13. Container class

(1) CCArray is similar to a container that stores arrays.

(2) CCDictionary is similar to the container corresponding to the key value of the hash table.

14. Drag CCMotionStreak

15. Drawing Images

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.