博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cocos creator长驻节点及其接口addPersistRootNode含义(挂到scene节点,并且添加不销毁标记)
阅读量:4204 次
发布时间:2019-05-26

本文共 898 字,大约阅读时间需要 2 分钟。

1)需求:比如网络管理类NetMgr.js

let NetMgr = cc.Class({    extends: cc.Component,    properties: {},    init() {        this.count = 0;        cc.log("init");    },    update(dt) {        this.count += dt;        if (this.count > 3) {            this.count = 0;            cc.log("update once");        }    },});NetMgr._instance = null;NetMgr.getInstance = () => {    if (!NetMgr._instance) {        let node = new cc.Node();        node.name = "NetMgrInstance";        NetMgr._instance = node.addComponent(NetMgr);        NetMgr._instance.init();        cc.game.addPersistRootNode(node);    }    return NetMgr._instance;}module.exports = NetMgr;

2)使用HelloWorld.js

let NetMgr = require("NetMgr")cc.Class({    extends: cc.Component,    start() {        NetMgr.getInstance();    },});

3)查看节点树,发现调用完addPersistRootNode后,就是把节点(节点上有和这个NetMgr组件)挂在了场景节点上了而已,然后挂了个不销毁的标记!

4)查看源码实现(发现就是把节点挂到了scene上,说明驻节点不能在Canvas里面!)

 

 

 

 

 

 

 

 

 

 

转载地址:http://tztli.baihongyu.com/

你可能感兴趣的文章