Cocos2d-x 3.0 drawing Node -- Node, cocos2d-xnode
* *********************************** Please specify the source: bytes ********************************************
I wrote a scenario switch yesterday, and it was about midnight .. Difficult to say...
***************************
As usual, first go to a directory:
1. Preface
2. View nodes from code
3. actually applied Node class
***************************
1. Preface
Old look, let's get something dry first
If you want to talk about Node, of course, you should first make a big dish-API
Node Description: A node is a basic element of a scenario diagram. The basic element of a scene map must be a Node object or a subclass of a Node object.
Here, we can see layers, MenuItem, Scene, Sprite, TMXTiledMap (parsing and rendering TMX maps), and particle system (particle system base class ).
Node is the root class of these classes
Note that these subclasses of Node can be nested with each other, Because:
The main features of a node are as follows:
- They can contain other node objects (
addChild
,getChildByTag
,removeChild
, Etc)
- They can schedule regular callbacks (
schedule
,unschedule
, Etc)
- They can perform some actions (
runAction
,stopAction
, Etc)
As mentioned in the first article, it can contain objects of other nodes. Therefore, child classes of nodes can be nested with each other.
We can also find that the Ref can be seen in many class inheritance diagrams,
Who are the Ref experts, so powerful?
Ref class, in fact, is 3.0 changed over the class name, originally called CCObject, it implements a set of memory management mechanism, therefore, all classes in the cocos2d-x are Ref subclass.
2. View nodes from code
Specifically, we can look at the code in the program,
Go to the definition of Scene and you can see:
<span style="font-family:Times New Roman;font-size:12px;">class CC_DLL Scene : public Node{public: /** creates a new Scene object */ static Scene *create(); // Overrides virtual Scene *getScene() override; using Node::addChild; virtual std::string getDescription() const override; CC_CONSTRUCTOR_ACCESS: Scene(); virtual ~Scene(); virtual bool init() override;protected: friend class Node; friend class ProtectedNode; friend class SpriteBatchNode; private: CC_DISALLOW_COPY_AND_ASSIGN(Scene); #if CC_USE_PHYSICSpublic: virtual void addChild(Node* child, int zOrder, int tag) override; virtual void update(float delta) override; inline PhysicsWorld* getPhysicsWorld() { return _physicsWorld; } static Scene *createWithPhysics(); CC_CONSTRUCTOR_ACCESS: bool initWithPhysics(); protected: void addChildToPhysicsWorld(Node* child); PhysicsWorld* _physicsWorld;#endif // CC_USE_PHYSICS};</span>
First, Scene is inherited from Node, so the scenario is also a Node, and the scenario has another anchor point than the Node. We can also see that there are addChild, update, and physical engines, as well as the addChildToPhysicsWorld newly added in 3.0.
Therefore, Scene is the root node of all scenarios and the root node of the number of nodes. Therefore, we must first create a scenario when drawing, then add something else to the scenario. Take HelloWorldScene as an example:
HelloWorldScene contains
-- Sprite (background image)
-- Menu)
-- LabelTTF (text layer)
Well, let's look at the Layer and go to the Layer definition:
<span style="font-size:18px;font-family: Arial;"> </span><span style="font-family:Times New Roman;font-size:10px;">// Deprecated touch callbacks. CC_DEPRECATED_ATTRIBUTE virtual bool ccTouchBegan(Touch *pTouch, Event *pEvent) final {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent); return false;}; CC_DEPRECATED_ATTRIBUTE virtual void ccTouchMoved(Touch *pTouch, Event *pEvent) final {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);} CC_DEPRECATED_ATTRIBUTE virtual void ccTouchEnded(Touch *pTouch, Event *pEvent) final {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);} CC_DEPRECATED_ATTRIBUTE virtual void ccTouchCancelled(Touch *pTouch, Event *pEvent) final {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);} CC_DEPRECATED_ATTRIBUTE virtual void ccTouchesBegan(__Set *pTouches, Event *pEvent) final {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);} CC_DEPRECATED_ATTRIBUTE virtual void ccTouchesMoved(__Set *pTouches, Event *pEvent) final {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);} CC_DEPRECATED_ATTRIBUTE virtual void ccTouchesEnded(__Set *pTouches, Event *pEvent) final {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);} CC_DEPRECATED_ATTRIBUTE virtual void ccTouchesCancelled(__Set *pTouches, Event *pEvent) final {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}</span>
By looking at these lines, we can see that Layer has more user interaction than Node, such as user touch (click, slide) messages and gravity sensing.
From the above two points, we can find that Node is a very important class.
To the definition of Node, you can see that it contains
-- Create is not required.
-- GetDescription
-- SetLocalZOrder (_ setLocalZOrder) sets the Z axis, that is, a scene may have multiple layers and arrange their upper and lower relations.
-- SetScale: zoom in and out. X is followed by zoom in and out of the X axis. If nothing is the overall zoom in, the parameter floating point type
-- SetPosition
-- SetSkew is distorted and used in the same Scale
-- SetAnchorPoint: Specifies the anchor point.
-- Whether the setVisible node is visible does not exist in the memory, but does not need to be drawn out.
-- SetRotation node Rotation
-- AddChild
-- GetChildByTag
-- SetParent: Set the parent node of the node
-- RemoveChild remove a node
-- SetUsersData: bind the node with data
-- OnEnter (onExit, resume, pause) node lifecycle Method
-- Cleanup: Stop all actions on the node
-- Visit recursively traverses all nodes
-- RunAction node binds an action
-- Update method of the node
-- ConvertToWorldSpace: Convert the coordinate system to the world coordinate system
And so on... A lot more... Tired
Many of the above are a series of examples, such:
Set indicates setting, and get indicates obtaining
... X indicates the operation on the X axis,... Y indicates the operation on the Y axis, and... Z indicates the operation on the Z axis.
To sum up, the Node class includes the following attributes:
---- Plot and transform attributes
- Location (default value: x = 0, y = 0)
- Zoom (default value: x = 1, y = 1)
- Rotate (in clockwise direction, in the unit of angle) (default value: 0)
- Anchor (default value: x = 0, y = 0)
- Content size (default value: width = 0, heigh = 0)
- Visibility (default value: true)
---- Child node (add, remove, and obtain)
---- Action (ActionManager)
---- Life Cycle (onEnter, onExit, resume, pause)
---- Node data (tag of a tag node, bind userData to data)
---- Schedule
Is it clear?
3. actually applied Node class
After talking about this, I have done everything. Come to practical use.
① Let's play with these positions, the anchor.
Create a new project and run:
Next, let's change the setposition of the background layer (sprite) to (0, 0 ):
// add "HelloWorld" splash screen" auto sprite = Sprite::create("HelloWorld.png"); // position the sprite on the center of the screen sprite->setPosition(0,0); // add the sprite as a child to this layer this->addChild(sprite, 0);
Run the following command again:
I found that the image is in the lower left corner. Why?
This is the anchor. As I said before, the anchor is equivalent to a nail in the center of the picture by default.
Therefore, we place the scenario at 0, 0, that is, the anchor point is aligned with the coordinate 0, 0.
Therefore, the image will be in the lower left corner. Do you still remember what it was like?
sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
VisibleSize is the size of the previously obtained screen:
Size visibleSize = Director::getInstance()->getVisibleSize();
Where is the original coordinate placed? In the middle of the screen, what are origin. x and origin. y?
Point origin = Director::getInstance()->getVisibleOrigin();
This is used to obtain the starting point coordinates of the visual area. The purpose is to adapt the data so that the data can be located in the same relative position at different resolutions.
So Let's adjust the anchor, isn't it?
Set the anchor to (0, 0 ):
sprite->setAnchorPoint(Point(0,0));
Run:
The anchor of the image is in the lower left corner, while the position is in the center of the screen. Therefore, the positional relationship is like this.
② Next, let's try the Node Action attributes.
Let's move a simple action ~
Create an action:
auto act=MoveTo::create(2,Point(0,0));
The first parameter is the time it takes to execute this action, and the second parameter is the location to which it is moved.
Then let sprite execute this action:
sprite->runAction(act);
Run the following command ~
Of course, not only sprite, but your HelloWorld word can also be executed. As long as it is a subclass of Node, this action can be executed,
Believe it or not, try it ~
auto act=MoveTo::create(2,Point(0,0));label->runAction(act);
OK, it's the sauce purple ~
A trip abroad will take place in two days. Traveling to Korea will take 10 days, which may not be updated.
* *********************************** Please specify the source: bytes ********************************************
How to optimize the drawing of CCSprite in cocos2d-x
You first use TexturePacker to generate a pvr diagram, then use CCSpriteBatchNode to load pvr, add CCSpriteBatchNode to the Layer, and add CCSprite to CCSpriteBatchNode after creating CCSprite, let CCSpriteNode optimize CCSprite on its own.
I don't know if you are satisfied with this answer.
What does auto mean in cocos2d-x 30?
Automatic Type Variable
For example, if you want to specify whether the variable is int or float, you can use auto now. When assigning values, the compiler automatically recognizes the type.
This is the new standard for C ++ 11. You can refer to the new standard document for C ++ 11.