当前位置:   article > 正文

Egret 对象池的使用_egret 怎么根据classname创建实例

egret 怎么根据classname创建实例

参考文档:https://www.cnblogs.com/gamedaybyday/p/6083164.html

完整项目:https://download.csdn.net/download/fanstasic/11926599

对象池类

  1. // 对象池
  2. class objectPool {
  3. private pool: Object;
  4. public constructor() {
  5. this.pool = {};
  6. }
  7. /**
  8. * 获取对象
  9. * @className 对象类名
  10. * @args 构造函数传参
  11. */
  12. public pop(className: string, ...args: any[]): any {
  13. if (this.pool[className] == null) {
  14. this.pool[className] = [];
  15. }
  16. var list: Array<any> = this.pool[className];
  17. if (list.length > 0) {
  18. return list.pop();
  19. } else {
  20. let argsLen: number = args.length;
  21. var clz: any = egret.getDefinitionByName(className);
  22. var obj: any;
  23. switch (argsLen) {
  24. case 0:
  25. obj = new clz();
  26. break;
  27. case 1:
  28. obj = new clz(args[0]);
  29. break;
  30. case 2:
  31. obj = new clz(args[0],args[1]);
  32. break;
  33. case 3:
  34. obj = new clz(args[0],args[1],args[2]);
  35. break;
  36. case 4:
  37. obj = new clz(args[0],args[1],args[2],args[3]);
  38. break;
  39. case 5:
  40. obj = new clz(args[0],args[1],args[2],args[3],args[4]);
  41. break;
  42. }
  43. obj.className = className;
  44. }
  45. return obj;
  46. }
  47. /**
  48. * 回收对象
  49. * @obj 需要回收的对象
  50. */
  51. public push(obj: any): void {
  52. var className = obj.className;
  53. if (this.pool[className] == null) {
  54. console.log("回收对象的数组不存在");
  55. return;
  56. }
  57. this.pool[className].push(obj);
  58. }
  59. /**
  60. * 创建对象(用于将要大量使用前,预先创建,防止使用时大量创建卡顿)
  61. * @className 对象类定义
  62. * @num 创建数量
  63. * @args 构造函数传参
  64. */
  65. public create(className: string, num: number, ...args: any[]) {
  66. var list = [];
  67. for (var i = 0; i < num; i++) {
  68. list.push(this.pop(className, ...args));
  69. }
  70. for (i = 0; i < num; i++) {
  71. this.push(list.pop());
  72. }
  73. }
  74. /**
  75. * 获取对象池对象数量
  76. * @className 对象类定义
  77. * @return 对象数量
  78. */
  79. public getLen(className: string): number {
  80. if (this.pool[className]) {
  81. return this.pool[className].length;
  82. }
  83. return 0;
  84. }
  85. /**
  86. * 清理对象
  87. * @className 对象类定义
  88.    * @funName 清理前执行指定函数
  89. */
  90. public clear(className: string, funName: string = null) {
  91. if (this.pool[className]) {
  92. funName && this.dealFun(className, funName);
  93. this.pool[className] = null;
  94. delete this.pool[className];
  95. }
  96. }
  97. /**
  98. * 对象池所有对象执行指定函数
  99. * @className 对象类定义
  100. * @funName 函数名
  101. */
  102. public dealFun(className: string, funName: string) {
  103. if (this.pool[className]) {
  104. var list: Array<any> = this.pool[className];
  105. var len = list.length;
  106. for (var i = 0; i < len; i++) {
  107. list[i][funName] && list[i][funName]();
  108. }
  109. }
  110. }
  111. }

调用类

  1. module game {
  2. export class mainScene extends eui.Component implements eui.UIComponent {
  3. private closeBtn: eui.Button;
  4. private player: game.player;
  5. private pool: objectPool;
  6. public constructor() {
  7. super();
  8. }
  9. protected partAdded(partName: string, instance: any): void {
  10. super.partAdded(partName, instance);
  11. }
  12. protected childrenCreated(): void {
  13. super.childrenCreated();
  14. this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.btnClick, this);
  15. this.showPlayer();
  16. }
  17. private btnClick(): void {
  18. let gameLogin = new game.gameLogin();
  19. game.sceneManager.Instance.changeScene(gameLogin);
  20. }
  21. private showPlayer(): void {
  22. this.player = new game.player();
  23. this.addChild(this.player);
  24. this.player.x = 100;
  25. this.player.y = 1000;
  26. this.pool = new objectPool();
  27. this.pool.create("game.bullet", 10);
  28. let timer: egret.Timer = new egret.Timer(1000);
  29. timer.addEventListener(egret.TimerEvent.TIMER, this.createBullet, this);
  30. timer.start();
  31. this.stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN, (evt: egret.TouchEvent) => {
  32. this.player.x = evt.localX;
  33. this.player.y = evt.localY;
  34. }, this);
  35. this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, (evt: egret.TouchEvent) => {
  36. this.player.x = evt.localX;
  37. this.player.y = evt.localY;
  38. }, this);
  39. }
  40. private createBullet() {
  41. let bull: game.bullet = this.pool.pop("game.bullet");
  42. this.addChild(bull);
  43. let timer: egret.Timer = new egret.Timer(20);
  44. timer.addEventListener(egret.TimerEvent.TIMER, function () {
  45. bull.bullet.y -= 10;
  46. }, this);
  47. timer.start();
  48. bull.bullet.x = this.player.x;
  49. bull.bullet.y = this.player.y;
  50. let ter = RES.getRes("bullet_png");
  51. bull.bullet.source = ter;
  52. setTimeout(() => {
  53. timer.stop();
  54. bull.bullet.source = "";
  55. this.pool.push(bull);
  56. this.removeChild(bull);
  57. }, 3000, this);
  58. let a = this.pool.getLen("game.bullet");
  59. console.log("this.pool.len = " + a);
  60. }
  61. }
  62. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/54418
推荐阅读
相关标签
  

闽ICP备14008679号