本文共 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/