当前位置:网站首页>Cocoscreator+typescripts write an object pool by themselves
Cocoscreator+typescripts write an object pool by themselves
2022-07-06 22:43:00 【Casimodo said】
Why object pools are needed ?
Create nodes at runtime (
cc.instantiate) And destroy (node.destroy) Operations are very performance intensive , So we are in a more complex scene , Usually only in the scenario initialization logic (onLoad) Nodes will be created in , Nodes are destroyed only when the scene is switched . If you make an action game with a large number of enemies or bullets that need to be generated and destroyed repeatedly , How can we create and destroy nodes at any time during the game ?
Many people will go to the official documents to check , Is there any way to solve ? Links to official documents about object pools :
Use object pool · Cocos Creator
https://docs.cocos.com/creator/2.3/manual/zh/scripting/pooling.html?q= So this time, I'd like to introduce my object pool :PoolMgr.ts
Define an object , Put the initialized object inside :
public static _dictPool: Object = {}initialization hui Object pool :
/**
* Initialize object pool
* @param count The amount generated
* @param prefab The prefabricated body that generates the node
*/
public static initPool(count:number,prefab:cc.Prefab){
for (let index = 0; index < count; index++) {
let node:cc.Node = cc.instantiate(prefab);
this.setNode(node);
}
}This is self written initialization , Generally, the object is initialized in the loading interface , You can call directly in the game .
Get an object in the object pool :
/**
* Access to the node
* @param prefab The prefabricated body that generates the node
* @param parent The parent of the node
* @returns Generate or nodes in the object pool
*/
public static getNode(prefab:cc.Prefab,parent:cc.Node){
let name = prefab.name;
let node:cc.Node = null;
if(this._dictPool[name]){
let pool = this._dictPool[name];
if (pool && pool.size() > 0) {
node = pool.get();
} else {
node = cc.instantiate(prefab);
}
}
else{
let pool = new cc.NodePool();
this._dictPool[name] = pool;
node = cc.instantiate(prefab);
}
node.parent = parent;
return node;
}Recycle an object :
/**
* Recycle node
* @param node Recycled nodes
*/
public static setNode(node:cc.Node){
const name = node.name;
let pool = null;
if(this._dictPool[name]){
pool = this._dictPool[name];
}
else{
pool = new cc.NodePool();
this._dictPool[name] = pool;
}
if(pool.size() > 100){
node.destroy();
return;
}
pool.put(node);
}Examples of use :
let count:number = 10;
let path:string = "xx";
sc.load.LoadPrefab(path,(prefab:cc.Prefab)=>{
PoolMgr.initPool(count,prefab);
});Call the initialization interface when loading resources , This initializes 10 Preform corresponding to the path ;
Set up :
let node = PoolMgr.getNode(prefab,parent);Obtain preform , Set parent node , You can get this node ;
Recycling :
PoolMgr.setNode(node);Pay attention when recycling nodes : If you make any changes to this node , For example, its rotation 、 Zoom, etc , Note its properties when used again .
边栏推荐
- CocosCreator+TypeScripts自己写一个对象池
- memcached
- The SQL response is slow. What are your troubleshooting ideas?
- OpenSSL: a full-featured toolkit for TLS and SSL protocols, and a general encryption library
- 欧洲生物信息研究所2021亮点报告发布:采用AlphaFold已预测出近1百万个蛋白质
- The difference between enumeration and define macro
- Signed and unsigned keywords
- 第十九章 使用工作队列管理器(二)
- 剑指offer刷题记录1
- Machine test question 1
猜你喜欢

Netxpert xg2 helps you solve the problem of "Cabling installation and maintenance"

自制J-Flash烧录工具——Qt调用jlinkARM.dll方式

UE4蓝图学习篇(四)--流程控制ForLoop和WhileLoop

Aardio - construct a multi button component with customplus library +plus

欧洲生物信息研究所2021亮点报告发布:采用AlphaFold已预测出近1百万个蛋白质

专为决策树打造,新加坡国立大学&清华大学联合提出快速安全的联邦学习新系统

Machine test question 1

Aardio - integrate variable values into a string of text through variable names

NPDP认证|产品经理如何跨职能/跨团队沟通?

Self made j-flash burning tool -- QT calls jlinkarm DLL mode
随机推荐
Netxpert xg2 helps you solve the problem of "Cabling installation and maintenance"
枚举与#define 宏的区别
NPDP certification | how do product managers communicate across functions / teams?
树的先序中序后序遍历
QT信号和槽
Project duplicate template
Senior soft test (Information System Project Manager) high frequency test site: project quality management
TypeScript获取函数参数类型
机试刷题1
变量与“零值”的比较
金融人士必读书籍系列之六:权益投资(基于cfa考试内容大纲和框架)
Windows auzre background operation interface of Microsoft's cloud computing products
自制J-Flash烧录工具——Qt调用jlinkARM.dll方式
Heavyweight news | softing fg-200 has obtained China 3C explosion-proof certification to provide safety assurance for customers' on-site testing
关于声子和热输运计算中BORN电荷和non-analytic修正的问题
Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)
How big is the empty structure?
OpenSSL: a full-featured toolkit for TLS and SSL protocols, and a general encryption library
BasicVSR_ Plusplus master test videos and pictures
2022-07-05 stonedb sub query processing parsing time analysis