讓你的類支援cc.pool
首先,你需在需要使用cc.pool來管理的類中實現reuse和unuse方法,cc.pool在執行putInPool時將調用該對象的unuse方法,可以在unuse中完成進入回收池前的操作,reuse是當你要從回收池中取出對象時的重新初始化操作,你可以將這個對象初始化為重新可用的狀態。
var MySprite = cc.Sprite.extend({ _hp: 0, _sp: 0, _mp: 0, ctor: function (f1, f2, f3) { this._super(f1, f2, f3); this.initData(f1, f2, f3); }, initData: function (f1, f2, f3) { this._hp = f1; this._mp = f2; this._sp = f3; }, unuse: function () { this._hp = 0; this._mp = 0; this._sp = 0; this.retain();//if in jsb this.setVisible(false); this.removeFromParent(true); }, reuse: function (f1, f2, f3) { this.initData(f1, f2, f3); this.setVisible(true); }}); MySprite.create = function (f1, f2, f3) { return new MySprite(f1, f2, f3)}MySprite.reCreate = function (f1, f2, f3) { var pool = cc.pool; if (pool.hasObject(MySprite)) return pool.getFromPool(MySprite, f1, f2, f3); return MySprite.create(f1, f2, f3);}