参考文档:https://www.cnblogs.com/gamedaybyday/p/6083164.html
完整项目:
对象池类
// 对象池
class objectPool {
private pool: Object;
public constructor() {
this.pool = {};
}
/**
* 获取对象
* @className 对象类名
* @args 构造函数传参
*/
public pop(className: string, ...args: any[]): any {
if (this.pool[className] == null) {
this.pool[className] = [];
}
var list: Array<any> = this.pool[className];
if (list.length > 0) {
return list.pop();
} else {
let argsLen: number = args.length;
var clz: any = egret.getDefinitionByName(className);
var obj: any;
switch (argsLen) {
case 0:
obj = new clz();
break;
case 1:
obj = new clz(args[0]);
break;
case 2:
obj = new clz(args[0],args[1]);
break;
case 3:
obj = new clz(args[0],args[1],args[2]);
break;
case 4:
obj = new clz(args[0],args[1],args[2],args[3]);
break;
case 5:
obj = new clz(args[0],args[1],args[2],args[3],args[4]);
break;
}
obj.className = className;
}
return obj;
}
/**
* 回收对象
* @obj 需要回收的对象
*/
public push(obj: any): void {
var className = obj.className;
if (this.pool[className] == null) {
console.log("回收对象的数组不存在");
return;
}
this.pool[className].push(obj);
}
/**
* 创建对象(用于将要大量使用前,预先创建,防止使用时大量创建卡顿)
* @className 对象类定义
* @num 创建数量
* @args 构造函数传参
*/
public create(className: string, num: number, ...args: any[]) {
var list = [];
for (var i = 0; i < num; i++) {
list.push(this.pop(className, ...args));
}
for (i = 0; i < num; i++) {
this.push(list.pop());
}
}
/**
* 获取对象池对象数量
* @className 对象类定义
* @return 对象数量
*/
public getLen(className: string): number {
if (this.pool[className]) {
return this.pool[className].length;
}
return 0;
}
/**
* 清理对象
* @className 对象类定义
* @funName 清理前执行指定函数
*/
public clear(className: string, funName: string = null) {
if (this.pool[className]) {
funName && this.dealFun(className, funName);
this.pool[className] = null;
delete this.pool[className];
}
}
/**
* 对象池所有对象执行指定函数
* @className 对象类定义
* @funName 函数名
*/
public dealFun(className: string, funName: string) {
if (this.pool[className]) {
var list: Array<any> = this.pool[className];
var len = list.length;
for (var i = 0; i < len; i++) {
list[i][funName] && list[i][funName]();
}
}
}
}
调用类
module game {
export class mainScene extends eui.Component implements eui.UIComponent {
private closeBtn: eui.Button;
private player: game.player;
private pool: objectPool;
public constructor() {
super();
}
protected partAdded(partName: string, instance: any): void {
super.partAdded(partName, instance);
}
protected childrenCreated(): void {
super.childrenCreated();
this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.btnClick, this);
this.showPlayer();
}
private btnClick(): void {
let gameLogin = new game.gameLogin();
game.sceneManager.Instance.changeScene(gameLogin);
}
private showPlayer(): void {
this.player = new game.player();
this.addChild(this.player);
this.player.x = 100;
this.player.y = 1000;
this.pool = new objectPool();
this.pool.create("game.bullet", 10);
let timer: egret.Timer = new egret.Timer(1000);
timer.addEventListener(egret.TimerEvent.TIMER, this.createBullet, this);
timer.start();
this.stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN, (evt: egret.TouchEvent) => {
this.player.x = evt.localX;
this.player.y = evt.localY;
}, this);
this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, (evt: egret.TouchEvent) => {
this.player.x = evt.localX;
this.player.y = evt.localY;
}, this);
}
private createBullet() {
let bull: game.bullet = this.pool.pop("game.bullet");
this.addChild(bull);
let timer: egret.Timer = new egret.Timer(20);
timer.addEventListener(egret.TimerEvent.TIMER, function () {
bull.bullet.y -= 10;
}, this);
timer.start();
bull.bullet.x = this.player.x;
bull.bullet.y = this.player.y;
let ter = RES.getRes("bullet_png");
bull.bullet.source = ter;
setTimeout(() => {
timer.stop();
bull.bullet.source = "";
this.pool.push(bull);
this.removeChild(bull);
}, 3000, this);
let a = this.pool.getLen("game.bullet");
console.log("this.pool.len = " + a);
}
}
}