1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| var SupportPoolHero = cc.Sprite.extend({ _hp : 0, _sp : 0, _mp : 0, ctor: function (hp, mp, sp) { this._super(res.sh_node_64_png); this.initData(hp, mp, sp); }, initData: function (hp, mp, sp) { this._hp, this._mp, this._sp = hp, mp, sp; }, unuse: function () { this._hp = this._mp = this._sp = 0; this.retain(); this.removeFromParent(); }, reuse: function (hp, mp, sp) { this.initData(hp, mp, sp); } });
SupportPoolHero.create = function (hp, mp, sp) { return new SupportPoolHero(hp, mp, sp) };
SupportPoolHero.reCreate = function (hp, mp, sp) { if (cc.pool.hasObject(SupportPoolHero)){ return cc.pool.getFromPool(SupportPoolHero, hp, mp, sp); }else{ return SupportPoolHero.create(hp, mp, sp); } };
var CCPoolLayer = cc.Layer.extend({ createCount : 3000, createHeroList : [], reCreateHeroList : [], timeList : {}, ctor : function(){ this._super(); this.loadMenu(); this.loadLabelInfo(); this.prepareHeroForPool(); }, loadMenu : function(){ var labCreate = new cc.LabelTTF("直接创建" + this.createCount + "个精灵!", "Arial", 24); var labReCreate = new cc.LabelTTF("使用pool创建" + this.createCount + "个精灵!", "Arial", 24);
var itemCreate = new cc.MenuItemLabel(labCreate, this.addSpriteByCreate, this); var itemReCreate = new cc.MenuItemLabel(labReCreate, this.addHeorByPool, this);
var menu = new cc.Menu(itemCreate, itemReCreate); menu.alignItemsHorizontallyWithPadding(150); this.addChild(menu); }, loadLabelInfo : function(){ this.labDirect = new cc.LabelTTF("直接创建耗费:", "Arial", 18); this.labDirect.setPosition(cc.pAdd(cc.visibleRect.center, cc.p(-230, -65))); this.labDirect.anchorY = 0; this.addChild(this.labDirect);
this.poolLabel = new cc.LabelTTF("使用pool创建耗费:", "Arial", 18); this.poolLabel.setPosition(cc.pAdd(cc.visibleRect.center, cc.p(200, -65))); this.poolLabel.anchorY = 0; this.addChild(this.poolLabel); }, prepareHeroForPool : function(){ for (var i = 0; i < this.createCount; i++) { var node = SupportPoolHero.create(0, 0, 0); this.addChild(node); cc.pool.putInPool(node); } }, addSpriteByCreate: function () { this.createHeroList = []; this.timeStart("directly"); for (var i = 0; i < this.createCount; i++) { var node = SupportPoolHero.create(1, 2, 3); this.createHeroList.push(node); this.addChild(node); node.setPosition(10 * i, cc.visibleRect.height * 0.8); } this.setDirectLabel(this.timeEnd("directly")); this.scheduleOnce(function(){ for (var i = 0; i < this.createHeroList.length; i++) { this.createHeroList[i].removeFromParent(true); } this.createHeroList = []; }, 0.5); }, addHeorByPool: function () { this.reCreateHeroList = []; this.timeStart("use Pool"); for (var i = 0; i < this.createCount; i++) { var node = SupportPoolHero.reCreate(4, 5, 6); this.reCreateHeroList.push(node); this.addChild(node); node.setPosition(10 * i, cc.visibleRect.height * 0.8); } this.setPoolLabel(this.timeEnd("use Pool")); this.scheduleOnce(function(){ for (var i = 0; i < this.reCreateHeroList.length; i++) { cc.pool.putInPool(this.reCreateHeroList[i]); } this.reCreateHeroList = []; }, 0.5); }, timeStart: function (name) { this.timeList[name] = {startTime: Date.now(), EndTime: 0, DeltaTime: 0}; }, timeEnd: function (name) { var obj = this.timeList[name]; obj.EndTime = Date.now(); obj.DeltaTime = obj.EndTime - obj.startTime; return obj.DeltaTime; }, setDirectLabel: function (time) { if (time == 0) { time = "<1"; } this.labDirect.setString("直接创建耗费:" + time + "ms"); }, setPoolLabel: function (time) { if (time == 0) { time = "<1"; } this.poolLabel.setString("使用pool创建耗费::" + time + "ms"); } });
|