Cocos2d-x Lua Node and Node hierarchy
Cocos2d-x Lua uses hierarchical (tree) structure to manage Node objects such as scenes, layers, Genie, menus, text, maps and particle systems. A scenario contains multiple layers, and one layer contains multiple objects such as Genie, menu, text, map, and particle system. Nodes in a hierarchy can be scenes, layers, Genie, menus, text, maps, particle systems, and other objects.
Shows the hierarchy of nodes.
Node hierarchy. These nodes have a common parent Node, as shown in the Node class diagram. The Node class is the most important root class of Cocos2d-x Lua, which is the root class of the class, such as scene, layer, Genie, menu, text, MAP and particle system.
Important operations in Node
As the root class, Node has many important functions. We will introduce them as follows:
Create a node. Local childNode = cc. Node: create ().
Add a new subnode. Node:-> addChild (childNode, 0,123). The second parameter is the order of drawing the Z axis, and the third parameter is the label.
Search for subnodes. Local node = node: getChildByTag (123), search for subnodes by TAG.
Node: removeChildByTag (123, true) deletes a subnode using the tag and stops all actions on the node.
Node: removeChild (childNode, true) Delete the childNode node. And stop all actions on the subnode.
Node: removeAllChildrenWithCleanup (true) deletes all subnodes of a node and stops all actions on these subnodes.
Node: removeFromParentAndCleanup (true): deletes a node from its parent node and stops all actions on the node.
Important attributes in Node
In addition, Node has two important attributes: position and anchorPoint.
The position attribute is the actual location of the Node object. The position attribute is often used together with the anchorPoint attribute. To accurately place a Node object (Standard rectangular image) on a certain position on the screen, you need to set the anchorPoint of the rectangle, anchorPoint is relative to position. The default value is (0.5, 0.5 ). Let's take a look at the following situations:
As shown in, the anchorPoint is (0.5, 0.5), which is the default situation.
If the anchorPoint is (0.5, 0.5), it indicates that the anchorPoint is (0.0, 0.0.
The anchorPoint is (0.0, 0.0), as shown in the following figure: the anchorPoint is (1.0, 1.0.
The anchorPoint is (1.0, 1.0), as shown in the following figure: the anchorPoint is (0.66, 0.5.
The anchorPoint is (0.66, 0.5). To learn more about the use of anchorPoint, we modify the HelloLua instance and GameScene. the GameScene: createLayer () function of lua is as follows. The bold font shows the code we added.
Function GameScene: createLayer ()
Cclog (GameScene init)
Local layer = cc. Layer: create ()
Local label = cc. LabelTTF: create (Hello World, Arial, 46)
Label: setPosition (cc. p (size. width/2,
Size. height-label: getContentSize (). height ))
Label: setAnchorPoint (cc. p (1.0, 1.0 ))
Layer: addChild (label)
Local bg = cc. Sprite: create(HelloWorld.png)
Bg: setPosition (cc. p (size. width/2, size. height/2 ))
Layer: addChild (bg)
Return layer
End
Shows the running result. The anchorPoint of the Hello World label is set to (1.0, 1.0 ).
The anchorPoint of the Hello World tag is (1.0, 1.0)
Game loop and Scheduling
Every game program is running in a loop, which is managed and maintained by the Director object. If you want the genie to be moved in the scenario, you can use sched in the game loop to schedule the running of objects such as the genie. Because the Node class encapsulates the Scheduler class, we can also directly use the timer-related functions in the Node.
The timer-related functions in Node mainly include:
ScheduleUpdateWithPriorityLua (nHandler, priority ). Each Node object only needs to call this function, and the Node object will call the nHandler function at a scheduled time every frame. Priority is the priority. The smaller the priority value, the more advanced the execution.
UnscheduleUpdate (). Stop scheduleUpdateWithPriorityLua scheduling.
To further understand the use of game loops and scheduling, we modify the HelloLua instance. Modify the GameScene. lua file using the following code:
Require Cocos2drequire Cocos2dConstantssize = cc. director: getInstance (): getWinSize () local label ① local GameScene = class (GameScene, function () return cc. scene: create () end) function GameScene. create () local scene = GameScene. new () scene: addChild (scene: createLayer () return sceneendfunction GameScene: ctor () end -- create layerfunction GameScene: createLayer () cclog (GameScene init) local layer = cc. layer: create () label = cc. labelTTF: create (Hello World, Arial, 46) label: setPosition (cc. p (size. width/2, size. height-label: getContentSize (). height) label: setTag (123) label: setAnchorPoint (cc. p (1.0, 1.0) layer: addChild (label) local bg = cc. sprite: create(HelloWorld.png) bg: setPosition (cc. p (size. width/2, size. height/2) layer: addChild (bg) local function update (delta) ② local x, y = label: getPosition () label: setPosition (cc. p (x + 2, y-2) end -- start game scheduling layer: scheduleUpdateWithPriorityLua (update, 0) ③ function onNodeEvent (tag) ④ if tag = exit then ⑤ -- start game scheduling layer: unscheduleUpdate () ⑥ end layer: registerScriptHandler (onNodeEvent) 7return layerendreturn GameScene
Line ① Of the above Code defines the module-level label object. The update (delta) function defined in row ② of the Code is a scheduling function. Line ③ code layer: scheduleUpdateWithPriorityLua (update, 0) enables game scheduling and schedules based on the frame rate. Priority 0 is the default value.