对象池技术
在arpg页游场景里面,经常被杀的怪物对象需要反复生成、销毁。频繁的触发垃圾回收会降低游戏运行效率,对象池技术就是为了解决这种问题
对象池特点:
1、聚合有一个容器,该容器装有已生成的对象;
2、封装对象的生成操作;
3、封装对象的销毁操作;
============================================================
==============简单的示例,可以自动扩容的对象池===========================
public class ObjectPool
{
protected var pool:Array = [];
protected var cl:Class;
private var property:Object;
private var count:int = 0;
public function ObjectPool(c:Class,property:Object = null)
{
this.cl = c;
this.property = property;
}
/**获取实例*/
public function getObject():*{
if (pool.length > 0){
return pool.pop();
}else{
trace("当前对象数目" + (++count));
return new cl;
}
}
/**回收实例,这里并没有对obj的属性进行处理,建议在回收前自行处理*/
public function recycle(obj:*):void{
pool.push(obj);
}
protected function initialize(ins:cl):void{
if (property){
for (var key:String in property){
if(ins.hasOwnProperty(key)){
ins[key] = property[key];
}
}
}
}
/**清空*/
public function release():void{
pool = [];
}
}