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

CentOS 8 officially released based on Red Hat Enterprise Linux 8

The CentOS Project, a 100% compatible rebuild of ...

Detailed explanation of several methods of deduplication in Javascript array

Table of contents Array deduplication 1 Double-la...

Vue implements accordion effect

This article example shares the specific code of ...

Detailed explanation of the six common constraint types in MySQL

Table of contents Preface 1.notnull 2. unique 3. ...

MySQL 5.7.10 installation and configuration tutorial under Windows

MySQL provides two different versions for differe...

Nginx uses Lua+Redis to dynamically block IP

1. Background In our daily website maintenance, w...

Loading animation implemented with CSS3

Achieve results Implementation Code <h1>123...

Method of implementing recursive components based on Vue technology

describe This article introduces a method to impl...

Complete steps to implement face recognition login in Ubuntu

1. Install Howdy: howdy project address sudo add-...

mysqldump parameters you may not know

In the previous article, it was mentioned that th...

js to achieve simulated shopping mall case

Friends who are learning HTML, CSS and JS front-e...

Linux Dig command usage

Dig Introduction: Dig is a tool that queries DNS ...

Understand CSS3 Grid layout in 10 minutes

Basic Introduction In the previous article, we in...