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
- CCLayer * layer1 = CCLayerColor: create (ccc4 (255, 0, 0,255), 300,200 );
- CCLayer * layer2 = CCLayerColor: create (ccc4 (255,255, 0,255), 100,100 );
- AddChild (layer1 );
- Layer1-> addChild (layer2 );
[Cpp]View plaincopy
- 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
- CCSprite * p1 = CCSprite: create ("HelloWorld.png", CCRectMake (100,100,100,100 ));
- CCSprite * p2 = CCSprite: create ("girl1.png ");
- AddChild (p1 );
- P1-> addChild (p2); // p2 serves as the child node of p1
- 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
- CCLayer * layer1 = CCLayerColor: create (ccc4 (255, 0, 0,255), 300,200 );
- CCLayer * layer2 = CCLayerColor: create (ccc4 (255,255, 0,255), 100,100 );
- AddChild (layer1 );
- Layer1-> addChild (layer2 );
- /* Layer1-> ignoreAnchorPointForPosition (false );*/
- Layer1-> setAnchorPoint (ccp (0.5, 0.5 ));
[Cpp]View plaincopy
- CCLayer * layer1 = CCLayerColor: create (ccc4 (255, 0, 0,255), 300,200 );
- CCLayer * layer2 = CCLayerColor: create (ccc4 (255,255, 0,255), 100,100 );
- AddChild (layer1 );
- Layer1-> addChild (layer2 );
- Layer1-> ignoreAnchorPointForPosition (false );
- Layer1-> setAnchorPoint (ccp (0.5, 0.5 ));
Sprite does not need to be set.
[Cpp]View plaincopy
- CCSprite * p1 = CCSprite: create ("HelloWorld.png", CCRectMake (100,100,100,100 ));
- CCSprite * p2 = CCSprite: create ("girl1.png ");
- AddChild (p1 );
- P1-> addChild (p2 );
- P2-> setAnchorPoint (ccp (1, 1 ));
- SetPosition (200,200 );
Compared with the third image above, we will understand it. The little girl's anchor has reached the red point.