CCNode of coco2d-x core class and coordinate system of coco2d-x

Source: Internet
Author: User

I. CCNode

CCNode node is the main class in the coco2d-x, inherit the CCObject,

Any object to be painted on the screen is CCNode, such as scene (CCScene), layout (CCLayer), menu (CCMenu), and Sprite)

Main features:

1. Periodic callback functions (Schedule, UnScheduled ......)

2. Each node can contain child nodes (the settings of the parent node can be passed to the child nodes)

3. There can be actions (CCAction)

4. CCNode does not contain textures.

5. The CCNode class does not contain textures. It can be used for location translation, scaling, and rotation changes.

Usage:

1. A parent node is required to manage a batch of child nodes.

2. You need to customize an object drawn on the screen. In this case, you can kneel down to inherit CCNode.

 

2. Coordinate System of coco2d-x

The Cocos2D-x is based on OpenGL and OpenGL ES. The origin of the coordinate system is in the lower left corner of the screen, the x axis is right, and the y axis is up.

World coordinate system: the world coordinate system is also called the absolute coordinate system. It is a concept established in game development. Therefore, the world is the game world. It establishes the reference standards required to describe other coordinate systems. We can use the world coordinate system to describe the positions of other coordinate systems.

Elements in the Cocos2D-x are parent-child hierarchical structures. The CCNode is used to set the position to the local coordinate system relative to its parent node, rather than the world coordinate system. Finally, when drawing the screen, the Cocos2D-x maps the local node coordinates of these elements into the world coordinate system coordinates. The orientation of the world coordinate system is the same as that of the OpenGL coordinate system. The origin is in the lower left corner of the screen, the x axis is right, and the y axis is up.

Anchor: The Anchor specifies the position of the point on which the texture maps and the node origin (that is, the point where the position is set) coincide. Therefore, the anchor makes sense only when the CCNode class nodes use the texture.

Node coordinate system: the coordinate system associated with a specific node. When a node moves or changes the direction, the coordinate system (ITS subnode) associated with the node moves or changes the direction. the CNode class uses the node Coordinate System of the parent node.

The CCNode classes include:

ConvertToWorldSpace: based on the current node? To the world coordinate system.

ConvertToNodeSpace: coordinates the world to the local coordinate system of the current node.

 

Iii. Instances

Create a new coco2d-x project, open HelloWorldScene. cpp, and replace the following code in the code in bool HelloWorld: init ():

Bool HelloWorld: init ()
{
Bool bRet = false;
Do
{
//////////////////////////////////////// //////////////////////////////////
// Super init first
//////////////////////////////////////// //////////////////////////////////
// Create a CCNode
CC_BREAK_IF (! CCLayer: init ());
CCNode * anode = CCNode: create ();

This-> addChild (anode, 0 );
// Add the exit menu
CCMenuItemImage * pCloseItem = CCMenuItemImage: create ("CloseNormal.png", "CloseSelected.png", this,
Menu_selector (HelloWorld: menuCloseCallback ));
PCloseItem-> setPosition (ccp (CCDirector: shareddire()-> getWinSize (). width-20, 20 ));

CCMenu * pMenu = CCMenu: create (pCloseItem, NULL );
PMenu-> setPosition (CCPointZero );
Anode-> addChild (pMenu, 1 );

// Add a text label
CCSize size = CCDirector: shareddire()-> getWinSize ();
CCLabelTTF * pLable = CCLabelTTF: create ("Hello COCO", "Thonburi", 34 );
PLable-> setPosition (ccp (size. width/2, size. height-20 ));
Anode-> addChild (pLable, 1 );


// Add a helloWorld sprite Image
CCSprite * pSprint = CCSprite: create ("HelloWorld.png ");
PSprint-> setPosition (ccp (size. width/2, size. height/2 ));
Anode-> addChild (pSprint, 0 );


Anode-> setPosition (200,200 );
// Set Scaling
Anode-> setScale (0.5 );
// Rotate
Anode-> setRotation (90.0 );
BRet = true;
} While (0 );


Return bRet;
}

Then compile and run, Structure

 

 

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.