How to use resident nodes for layer management in CocosCreator

How to use resident nodes for layer management in CocosCreator

CocosCreator version: 2.3.4

Most games have layer management, such as

  • sceneLayer scene layer
  • panelLayer pop-up layer
  • tipLayer Tip box layer

The scenes in Cocos are not persistent and will be automatically destroyed every time you switch. If you put these layers on the scene, do you have to put them once for each scene? And then get it again, which is very troublesome.

The scene is loaded using cc.director.loadScene. The container node of the scene seems to be a nodeActivator on the director.

Now let's not consider the scene container or the top-level container of Cocos. I can think of two approaches to layer management.

1. There is only one scene

The whole game has one scene, which is the scene at the entrance of the game. On this scene, nodes of layers such as sceneLayer are placed. This entrance scene is equivalent to the stage of egret and laya.

Then all scenes and pop-up modules are made into prefabs, and each time they are displayed, just addChild to the corresponding layer of the entry scene.

2. Use resident nodes

For example, I put layers such as sceneLayer in scene 1. For easier display, I added a single color to each layer.

The resident node must be under the root node, that is, at the same level as the canvas. Set the three layers as permanent nodes.

onLoad(){
    cc.game.addPersistRootNode(cc.find("sceneLayer"));
    cc.game.addPersistRootNode(cc.find("panelLayer"));
    cc.game.addPersistRootNode(cc.find("tipLayer"));
}

Then switch the scene. In the new scene, you can still display and obtain layers such as sceneLayer.

onLoad(){
    console.log(cc.find("sceneLayer")); //Output sceneLayer's cc.Node
}

Using resident nodes, we can place layers such as sceneLayer in the entrance scene. Use the layer management class to save the reference.

III. Best Practices

Layer management class, singleton

export default class LayerManager extends cc.Component {

    private static instance:LayerManager;
    public static ins():LayerManager{
        if (this.instance == null) {
            this.instance = new LayerManager();
        }
        return this.instance;
    }

    public panelLayer:cc.Node;
    public tipLayer:cc.Node;
    
}

Set a resident node layer in the entry scene and use the layer management class to save the reference. For later use.

@ccclass
export default class Helloworld extends cc.Component {

    onLoad(){
        cc.game.addPersistRootNode(cc.find("sceneLayer"));
        cc.game.addPersistRootNode(cc.find("panelLayer"));
        cc.game.addPersistRootNode(cc.find("tipLayer"));

        LayerManager.ins().panelLayer = cc.find("panelLayer");
        LayerManager.ins().tipLayer = cc.find("tipLayer");
    }
}

The above is the details of how to use resident nodes for layer management in CocosCreator. For more information about CocosCreator resident nodes for layer management, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • CocosCreator Getting Started Tutorial: Network Communication
  • How to create WeChat games with CocosCreator
  • Detailed explanation of how CocosCreator system events are generated and triggered
  • How to use a game controller in CocosCreator
  • Detailed explanation of CocosCreator Huarongdao digital puzzle
  • Detailed explanation of the fish school algorithm in CocosCreator game
  • Detailed explanation of CocosCreator optimization DrawCall
  • CocosCreator implements skill cooling effect
  • Detailed explanation of cocoscreater prefab
  • Detailed explanation of CocosCreator message distribution mechanism

<<:  In-depth understanding of umask in new linux file permission settings

>>:  How to transfer files between Docker container and local machine

Recommend

Detailed explanation of the code for implementing linear gradients with CSS3

Preface The gradient of the old version of the br...

Details of Linux file descriptors, file pointers, and inodes

Table of contents Linux--File descriptor, file po...

Detailed use cases of MySql escape

MySQL escape Escape means the original semantics ...

Detailed explanation of Nginx process management and reloading principles

Process structure diagram Nginx is a multi-proces...

Detailed steps for building a React application with a Rails API

Table of contents Backend: Rails API part Front-e...

Example of using Nginx to implement port forwarding TCP proxy

Table of contents Demand Background Why use Nginx...

Computed properties and listeners details

Table of contents 1. Calculated properties 1.1 Ba...

Tutorial for installing MySQL 8.0.18 under Windows (Community Edition)

This article briefly introduces how to install My...

MySQL performance optimization tips

MySQL Performance Optimization MySQL is widely us...

Docker advanced method of rapid expansion

1. Command method Run the nginx service in the cr...

MySQL SQL statement performance tuning simple example

MySQL SQL statement performance tuning simple exa...

Detailed explanation of the role and principle of key in Vue

Table of contents 1. Let’s start with the conclus...

Vue realizes screen adaptation of large screen pages

This article shares the specific code of Vue to a...

A few front-end practice summaries of Alipay's new homepage

Of course, it also includes some personal experien...