rankItems.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Learn cc.Class:
  2. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
  3. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
  4. // Learn Attribute:
  5. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
  6. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
  7. // Learn life-cycle callbacks:
  8. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
  9. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
  10. window.rankItems = {
  11. }
  12. cc.Class({
  13. extends: cc.Component,
  14. properties: {
  15. // foo: {
  16. // // ATTRIBUTES:
  17. // default: null, // The default value will be used only when the component attaching
  18. // // to a node for the first time
  19. // type: cc.SpriteFrame, // optional, default is typeof default
  20. // serializable: true, // optional, default is true
  21. // },
  22. // bar: {
  23. // get () {
  24. // return this._bar;
  25. // },
  26. // set (value) {
  27. // this._bar = value;
  28. // }
  29. // },
  30. item: cc.Prefab,
  31. },
  32. // LIFE-CYCLE CALLBACKS:
  33. onLoad () {
  34. this.pool = new cc.NodePool();
  35. rankItems = this;
  36. Log.info("走 ",this.pool,rankItems)
  37. },
  38. start () {
  39. },
  40. getItem(){
  41. let item = null;
  42. //c1、当前对象池中的可用对象数量
  43. if (this.pool.size > 0) {
  44. //_1、从对象池中获取对象
  45. item = this.pool.get();
  46. } else {
  47. //_2、若没有空闲的对象,也就是对象不够用时,就克隆节点
  48. item = cc.instantiate(this.item);
  49. }
  50. return item;
  51. },
  52. removeItem(item){
  53. Log.info("放进去了吗",item)
  54. this.pool.put(item);
  55. }
  56. // update (dt) {},
  57. });