ZORDER and Tag in Cocos2d-X
ZORDER: the value that describes the rendering order. Each CCNode has ZORDER. The default value is 0.
The larger the ZORDER, the more draw it after it.
If ZORDER is the same, the arrival sequence is used to draw the first added node.
ZORDER can only be compared between nodes with the same parent node
Start with a simple piece of code:
// Set the attribute bool Zorder: init () {// initialize the CCLayer: init () of the parent class layer; // obtain the window size CCSize winSize = CCDirector :: shareddire()-> getWinSize (); // create the sprite * sprite1 = CCSprite: create ("CloseNormal.png"); addChild (sprite1 ); // set sprite1-> setPosition (ccp (winSize. width/2, winSize. height/2); CCSprite * sprite2 = CCSprite: create ("HelloWorld.png"); addChild (sprite2); // set the sprite2 position> setPosition (ccp (winSize. width/2, winSize. height/2); return true ;}
I am not familiar with the above Code. The above code is implemented. Two elves are created in the layer, and the positions of the two elves are the same, so this problem occurs in the program. Which genie is displayed first?
To solve this problem, let's take a look at the rendering model of the code above.
According to the above model, we can see that the Layer is CCNode, and Sprite1 and Sprite2 are two subnodes of CCNode. Therefore, we can see that the ZORDER of Sprite1 and Sprite2 are both 0, and will be drawn after the addition, so the following genie is displayed
Execution result:
Modify the ZORDER value:
Method 1: Modify the ZORDER value by modifying the second parameter of the addChild function.
In the above Code
// Create the genie CCSprite * sprite1 = CCSprite: create ("CloseNormal.png"); addChild (sprite1 );
Change
// Create the genie CCSprite * sprite1 = CCSprite: create ("CloseNormal.png"); addChild (sprite1, 1 );
Execution result:
Method 2: Use the setZOrder function to set the ZORDER Value
Add
// Use the function to set ZORDER sprite1-> setZOrder (1 );
Execution result:
A node ID card. You can use getChildByTag to obtain the Node object.
It is used to reduce member variables.
The Tag is valid only between nodes with the same parent node.
The Tag cannot be the same among the sons of the same parent node. If the Tag is the same, cocos will not report an error. getChildByTag may obtain a CCNode that is not your desired
Generally, enumeration is used to indicate tags to prevent duplication.
Tags are sometimes used when IDs are used, especially when multiple menu items use the same response function.
Zookeeper