/**
* Author:W
* 对象池
*/
cc.Class({
extends: cc.Component,
properties: {
enmyPrefab:{
default:null,
type:cc.Prefab,
}
},
// LIFE-CYCLE CALLBACKS:
onLoad () {
this.initPool();
},
start () {
},
//敌人缓冲池的初始化
initPool:function(){
this.enemyPool = new cc.NodePool();
var size = 10;
for(var i=0;i<size;++i)
{
var enemy = cc.instantiate(this.enmyPrefab);
this.enemyPool.put(enemy);
}
},
//从对象池中获取一个敌人
getEnemy:function(){
var enemy = null;
if(this.enemyPool.size()>0)
{
enemy = this.enemyPool.get();
}else
{
enemy = cc.instantiate(this.enmyPrefab);
}
return enemy;
},
//把已经死亡的敌人对象返回对象池中
backEnemy:function(enemyKilled){
this.enemyPool.put(enemyKilled);
},
//清空对象池
clearEnemyPool:function(){
this.enemyPool.clear();
}
// update (dt) {},
});
对象池的使用
最新推荐文章于 2023-11-30 17:08:08 发布