CCNode is a class in the C++ programming language that belongs to the Cocos2d-x game development framework library. CCNode represents a node in the scene graph of a game, and it provides various methods and properties to control the behavior and appearance of the node.
One of the useful methods of CCNode is setAnchorPoint, which allows you to set the position of the node's anchor point. The anchor point is a point within the node's bounds that serves as a reference for positioning and scaling the node. By default, the anchor point is at the center of the node.
Here are some code examples that demonstrate the usage of setAnchorPoint in Cocos2d-x:
// Create a sprite and set its anchor point to the bottom-left corner auto sprite = Sprite::create("my_sprite.png"); sprite->setAnchorPoint(Vec2::ZERO);
// Create a label and set its anchor point to the top-right corner auto label = Label::createWithTTF("Hello, world!", "Arial.ttf", 24); label->setAnchorPoint(Vec2(1.0f, 1.0f));
// Create a custom node and set its anchor point to the center-bottom position class MyNode : public Node { public: CREATE_FUNC(MyNode); virtual bool init() override { if (!Node::init()) { return false; } // Add some child nodes to this custom node // ... this->setAnchorPoint(Vec2(0.5f, 0.0f)); // Set the anchor point to (center, bottom) return true; } }; auto mynode = MyNode::create();
In the first example, we create a sprite and set its anchor point to the bottom-left corner (0,0) using the Vec2::ZERO constant. This means that when we position the sprite on the screen, its bottom-left corner will be aligned with the position we set.
In the second example, we create a label and set its anchor point to the top-right corner (1,1) using a Vec2 constructor. This means that when we position the label on the screen, its top-right corner will be aligned with the position we set.
In the third example, we create a custom node class and override its init method to set its anchor point to (0.5,0) using a Vec2 constructor. This means that when we position the node on the screen, its center-bottom position will be aligned with the position we set.
C++ (Cpp) CCNode::setAnchorPoint - 19 examples found. These are the top rated real world C++ (Cpp) examples of CCNode::setAnchorPoint extracted from open source projects. You can rate examples to help us improve the quality of examples.