Cocos2d-x_AnchorPoint anchor, cocos2d anchor

Source: Internet
Author: User

Cocos2d-x_AnchorPoint anchor, cocos2d anchor

The anchor is the focus of positioning and transformation operations. The anchor can be seen as the point where a piece of paper or photo is pinned to the wall with a graph nail.

The node location is determined by the position and anchor point we set.

It is worth mentioning that the default position of anchor point is the lower left, while that of other nodes is the midpoint.

The origin of a node is the lower left corner of the parent node.

[Cpp]View plaincopy
  1. CCLayer * layer1 = CCLayerColor: create (ccc4 (255, 0, 0,255), 300,200 );
  2. CCLayer * layer2 = CCLayerColor: create (ccc4 (255,255, 0,255), 100,100 );
  3. AddChild (layer1 );
  4. Layer1-> addChild (layer2 );

[Cpp]View plaincopy
  1. Layer2-> setPosition (50, 50 );

It can be seen that the anchor of the Layer is in the lower left corner, and the origin of the node is in the lower left corner of the parent node.

Let's look at another one:

[Cpp]View plaincopy
  1. CCSprite * p1 = CCSprite: create ("HelloWorld.png", CCRectMake (100,100,100,100 ));
  2. CCSprite * p2 = CCSprite: create ("girl1.png ");
  3. AddChild (p1 );
  4. P1-> addChild (p2); // p2 serves as the child node of p1
  5. SetPosition (200,200); // The Origin of the Layer is moved to 200,200


Set the anchor:

The Layer and Sprite are different. To set an anchor, you must first:IgnoreAnchorPointForPosition (false );

[Cpp]View plaincopy
  1. CCLayer * layer1 = CCLayerColor: create (ccc4 (255, 0, 0,255), 300,200 );
  2. CCLayer * layer2 = CCLayerColor: create (ccc4 (255,255, 0,255), 100,100 );
  3. AddChild (layer1 );
  4. Layer1-> addChild (layer2 );
  5. /* Layer1-> ignoreAnchorPointForPosition (false );*/
  6. Layer1-> setAnchorPoint (ccp (0.5, 0.5 ));

[Cpp]View plaincopy
  1. CCLayer * layer1 = CCLayerColor: create (ccc4 (255, 0, 0,255), 300,200 );
  2. CCLayer * layer2 = CCLayerColor: create (ccc4 (255,255, 0,255), 100,100 );
  3. AddChild (layer1 );
  4. Layer1-> addChild (layer2 );
  5. Layer1-> ignoreAnchorPointForPosition (false );
  6. Layer1-> setAnchorPoint (ccp (0.5, 0.5 ));

Sprite does not need to be set.

[Cpp]View plaincopy
  1. CCSprite * p1 = CCSprite: create ("HelloWorld.png", CCRectMake (100,100,100,100 ));
  2. CCSprite * p2 = CCSprite: create ("girl1.png ");
  3. AddChild (p1 );
  4. P1-> addChild (p2 );
  5. P2-> setAnchorPoint (ccp (1, 1 ));
  6. SetPosition (200,200 );

Compared with the third image above, we will understand it. The little girl's anchor has reached the red point.

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.