Node management
Similar to HTML, the entire HTML is composed of various nodes. The root node is the HTML tag, and then there are the child nodes, and then the child nodes nest the child nodes to form a node tree. Then the browser renders based on this node tree, and then The page we need is rendered, and Laya is actually the same. Different components and scenes form a node tree, which is rendered by the Laya engine.
Nodes are the basis of project development. Only by fully understanding how to operate nodes can complex requirements be realized faster. Each node component class inherits from the Node class, which is the base class for all objects that can be placed in the display list. This display list manages all objects displayed in the Laya runtime. Use the Node class to arrange display objects in a display list. Node objects can have child display objects.
Let’s get familiar with all the basic functions of the Node class
Add to
Without considering the hierarchy, we can add child nodes to a node, so that the child nodes can follow the parent node to display or move, and facilitate management. The added child nodes will be added to the end of all child nodes.
It is often necessary to consider the hierarchical relationship or occlusion relationship of nodes, and if you already know that you need to add it to the specified hierarchical position, you can use this method.
Directly adding a group of child nodes to the parent node in order is equivalent to calling the addChild child node once in a loop.
Find
When we know the name of a node, we can use this method to find a child node one level below the parent node.
Get the child node object according to the index position of the child node getChildAt(index:number)
When we know the index position of a node, we can use this method to find a child node one level below the parent node.
Get the index position of the child node according to the child node object getChildIndex(node:Node)
When we want to know the index position of a node, we can use this method to get the child nodes
By calling the numChildren attribute of the parent node, you can get the number of all child nodes at the next level of the parent node, which can be used to traverse all child nodes. Note that the child nodes under the child node are not included.
You can directly obtain the parent node by calling the parent attribute of the child node.
To determine whether a node contains a child node, you can use this method. Note that this search process will traverse all nested child nodes.
Change
If you want to change the display level of child nodes, you can call this method. Note that if the index exceeds the number of nodes of the parent node, an exception will be thrown.
Pass in the new node and replace it at the index position of the existing child node replaceChild(newNode: Node, oldNode: Node)
If you want to replace an existing node with a new node, you can call this method. Note that the original child node will be removed but will not be destroyed.
Remove
This method is called when deleting a child node. Note that the child node will be removed but will not be destroyed.
When you do not need to know the parent node, you can directly delete yourself from the parent node by calling this method. This method is used more frequently.
When we know the name of a node, we can use this method to delete the child node from the parent node.
When we know the index position of a node, we can use this method to delete the child node from the parent node
Delete all child nodes in the specified index range removeChildren(beginIndex: number = 0, endIndex: number = 0x7fffffff)
This method can quickly delete all child nodes in the specified index range