Creating and Destroying Nodes

Creating a New Node

In addition to creating nodes through the scene editor, it can also dynamically create nodes in scripts using new Node() and adding it to the scene. Example:

  1. import { _decorator, Component, Node } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass("test")
  4. export class test extends Component {
  5. start(){
  6. let node =new Node('box');
  7. node.setPosition(0,0,-10);
  8. }
  9. }

Cloning an Existing Node

Sometimes it is needed to dynamically clone the existing nodes in the scene, it can be done through the instantiate method. Example:

  1. import { _decorator, Component, Node,instantiate, director } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass("test")
  4. export class test extends Component {
  5. @property({type:Node})
  6. private target: Node = null;
  7. start(){
  8. let scene = director.getScene();
  9. let node = instantiate(this.target);
  10. node.parent = scene;
  11. node.setPosition(0, 0,-10);
  12. }
  13. }

Creating a Prefab Node

Similar to cloning an existing node, you can set a prefab (Prefab) and generate a node through instantiate. Example:

  1. import { _decorator, Component, Prefab, instantiate, director } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass("test")
  4. export class test extends Component {
  5. @property({type:Prefab})
  6. private target: Prefab = null;
  7. start(){
  8. let scene = director.getScene();
  9. let node = instantiate(this.target);
  10. node.parent = scene;
  11. node.setPosition(0,0,0);
  12. }
  13. }

Destroy the node

Through the node.destroy() function, nodes can be destroyed. It is worth mentioning that the destroyed node will not be removed immediately, but will be executed uniformly after the logic update of the current frame is completed. When a node is destroyed, the node is in an invalid state. Use isValid to determine whether the current node has been destroyed. Example:

  1. import { _decorator, Component, Node } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass("test")
  4. export class test extends Component {
  5. @property({type:Node})
  6. private target: Node = null;
  7. private positionz: number = -20;
  8. start(){
  9. // Destroy the node after 5 seconds
  10. setTimeout(function () {
  11. this.target.destroy();
  12. }.bind(this), 5000);
  13. }
  14. update(deltaTime: number){
  15. console.info(this.target.isValid);
  16. this.positionz += 1*deltaTime;
  17. if (this.target.isValid) {
  18. this.target.setPosition(0.0,0.0,this.positionz);
  19. }
  20. }
  21. }

The Difference Between destroy and removeFromParent

After calling removeFromParent on a node, the node is not released from memory because the engine still holds its data internally. If a node is no longer used, please call its destroy directly instead of removeFromParent, otherwise, a memory leak will result.

In short, if a node is no longer used, destroy is right, there is no need to removeFromParent nor to set parent to null.