Cocos2d-x Lua node-and node-level architecture

Source: Internet
Author: User
Tags addchild

Cocos2d-x Lua node-and node-level architecture

Cocos2d-x Lua uses a hierarchical (tree) structure to manage nodes (node) objects such as scenes, layers, sprites, menus, text, maps, and particle systems. A scene contains multiple layers, and a layer contains objects such as sprites, menus, text, maps, and particle systems. Nodes in a hierarchy can be any object, such as scenes, layers, sprites, menus, text, maps, and particle systems.
The hierarchical structure of the nodes is shown.

Node hierarchy These nodes have a common parent class Node,node class diagram as shown in. The node class is the most important root class for cocos2d-x Lua, which is the root of classes such as scenes, layers, sprites, menus, text, maps, and particle systems. Node class diagram important operations in node
node as the root class it has a lot of important functions below we introduce each:
Create a node. Local Childnode = cc. Node:create ().
Adds a new child node. Node:->addchild (childnode, 0, 123), the second parameter is plotted in the z-axis order, and the third parameter is a label.
Find child nodes. Local node = Node:getchildbytag (123), looking up child nodes via tags.
Node:removechildbytag (123, true) removes the child node from the label and stops all actions on that node.
Node:removechild (Childnode, True) deletes the Childnode node. And stop all actions on that child node.
Node:removeallchildrenwithcleanup (true) removes all child nodes of the node and stops all actions on those child nodes.
Node:removefromparentandcleanup (TRUE) removes the node from the parent node and stops all actions on that node.


Important attributes in node
In addition, node has two very important attributes: position and Anchorpoint.
The position (position) property is the actual location of the node object. The Position property is often used in conjunction with the Anchorpoint property, in order to place a node object (the standard rectangular shape) exactly on the screen, you need to set the anchor point of the rectangle, Anchorpoint is relative to the position ratio, the default is ( 0.5,0.5). Let's take a look at some of the following scenarios:
As shown in the Anchorpoint (0.5,0.5) case, this is the default condition.

The Anchorpoint for (0.5,0.5) is shown to be Anchorpoint (0.0,0.0) case.


Anchorpoint for (0.0,0.0) as shown is anchorpoint for (1.0,1.0) case.



Anchorpoint for (1.0,1.0) as shown is anchorpoint for (0.66, 0.5) case.

Anchorpoint for (0.66, 0.5) to further understand the use of anchorpoint, we modify the Hellolua instance, modify the Gamescene.lua Gamescene:createlayer () function as follows, where bold fonts show 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
As shown in the run results, the Hello World tag is set to Anchorpoint (1.0,1.0).

The anchorpoint of the Hello World Tag is (1.0,1.0)

Game Loop and Scheduling
Every game program has a loop running continuously and it is maintained by the Director object to manage very much. If you need the sprites in the scene to move, we can use Timers (Scheduler) to schedule the operation of objects such as sprites in the game loop. Because the node class encapsulates the scheduler class, we can also use the timer-related functions in node directly.
The timer-related functions in node include:
Scheduleupdatewithprioritylua (Nhandler, priority). As soon as each node object calls the function, the node object will periodically call the Nhandler function back every frame. Priority is the precedence, and the smaller the value, the more it executes first.
Unscheduleupdate (). Stops the dispatch of the Scheduleupdatewithprioritylua.
To further understand the use of game loops and schedules, we modify Hellolua instances. Modify the Gamescene.lua file with the following code: [HTML]View Plaincopy
  1. <span style="font-size:14px;font-weight:normal;" >require "cocos2d"
  2. Require "cocos2dconstants"
  3. Size = cc. Director:getinstance (): Getwinsize ()
  4. Local Label①
  5. Local Gamescene = Class ("Gamescene", function ()
  6. return CC. Scene:create ()
  7. End
  8. function Gamescene.create ()
  9. Local scene = gamescene.new ()
  10. Scene:addchild (Scene:createlayer ())
  11. Return scene
  12. End
  13. function Gamescene:ctor ()
  14. End
  15. --Create Layer
  16. function Gamescene:createlayer ()
  17. Cclog ("Gamescene init")
  18. Local layer = cc. Layer:create ()
  19. label = CC. Labelttf:create ("Hello World", "Arial", "$")
  20. Label:setposition (CC.P (SIZE.WIDTH/2,
  21. Size.height-label:getcontentsize (). Height))
  22. Label:settag (123)
  23. Label:setanchorpoint (CC.P (1.0, 1.0))
  24. Layer:addchild (label)
  25. Local bg = cc. Sprite:create ("Helloworld.png")
  26. Bg:setposition (CC.P (SIZE.WIDTH/2, SIZE.HEIGHT/2))
  27. Layer:addchild (BG)
  28. Local function Update (delta) ②
  29. Local x,y = label:getposition ()
  30. Label:setposition (CC.P (x + 2, y-2))
  31. End
  32. --Start Game scheduling
  33. Layer:scheduleupdatewithprioritylua (update, 0) ③
  34. function onnodeevent (tag) ④
  35. If tag = = "Exit" Then⑤
  36. --Start Game scheduling
  37. Layer:unscheduleupdate () ⑥
  38. End
  39. End
  40. Layer:registerscripthandler (onnodeevent) ⑦
  41. return layer
  42. End
  43. return gamescene</span>
The above Code section ① defines a module-level label object label. The update (delta) function defined by the Code section ② is a dispatch function. Line ③ Code layer:scheduleupdatewithprioritylua (update, 0) is the game scheduling, according to the frame rate scheduling, priority 0 is the default value.
The ④ Line code is the layer handling event callback function, where the ⑤ line code is judged to be an exit layer event, and if it is an exit layer event, call the ⑥ line code to stop the dispatch. Line ⑦ Code Layer:registerscripthandler (onnodeevent) is the registration layer event listener.

Cocos2d-x Lua node-and node-level architecture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.