当前位置:网站首页>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 Creatorhttps://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 .
边栏推荐
- Web APIs DOM 时间对象
- three. JS gorgeous bubble effect
- Classification, function and usage of MySQL constraints
- 空结构体多大?
- Self made j-flash burning tool -- QT calls jlinkarm DLL mode
- MATLAB小技巧(27)灰色预测
- Config:invalid signature solution and troubleshooting details
- 机试刷题1
- Plafond du tutoriel MySQL, bien collecté, regardez lentement
- [leetcode] 19. Delete the penultimate node of the linked list
猜你喜欢
Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)
Pit encountered by handwritten ABA
剑指offer刷题记录1
关于声子和热输运计算中BORN电荷和non-analytic修正的问题
The SQL response is slow. What are your troubleshooting ideas?
Should novice programmers memorize code?
树的先序中序后序遍历
Web APIs DOM 时间对象
pytorch_YOLOX剪枝【附代码】
Aardio - 利用customPlus库+plus构造一个多按钮组件
随机推荐
Matlab tips (27) grey prediction
extern关键字
sizeof关键字
UVa 11732 – strcmp() Anyone?
General implementation and encapsulation of go diversified timing tasks
(18) LCD1602 experiment
Puppeter connects to the existing Chrome browser
MySQL数据库基本操作-DML
Unity3d minigame unity webgl transform plug-in converts wechat games to use dlopen, you need to use embedded 's problem
rust知识思维导图xmind
cuda 探索
Build op-tee development environment based on qemuv8
How do I write Flask's excellent debug log message to a file in production?
[IELTS speaking] Anna's oral learning record part1
npm无法安装sharp
DR-Net: dual-rotation network with feature map enhancement for medical image segmentation
Daily question 1: force deduction: 225: realize stack with queue
Adavit -- dynamic network with adaptive selection of computing structure
The ceiling of MySQL tutorial. Collect it and take your time
自定义 swap 函数