"COCOS2D-JS Official Documents" 12, object buffer pool December 12

Source: Internet
Author: User

Cc.pool Usage Scenarios
    • Elements that are often created and destroyed, such as bullets inside a flying game.
    • Non-applicable scenarios: objects that are not very often created, such as backgrounds, buildings, etc.
How to use Cc.pool
  1. Let your class supportcc.pool

    First, you need to use thecc.poolImplemented in the class to be managedreuseAndunuseMethodcc.poolIn the executionputInPoolwill be called when the object'sunuseMethod that can be used inunuseBefore entering the Recycle pool,reuseIs the reinitialization when you want to remove objects from the recycle pool, and you can initialize the object to a state that is re-usable.

    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);}
  2. Put into the recycling pool

    cc.pool.putInPool(object);

    Call this method to invoke the object's unuse method and put the object into the recycle pool.

  3. Reclaim objects from the Recycle pool

    var object = cc.pool.getFromPool("MySprite", args);

    When you need to fetch an object from the Recycle pool, you can call the class of the getFromPool incoming object and pass in the initialization parameters that need to be passed in, which will be passed into the reuse method, and the cc.pool reuse method will be called automatically.

  4. Determine if an object is available in the Recycle pool

    var exist = Cc.pool.hasObject (" MySprite ");  

    This method is used to find out if there is

  5. Delete an object in the Recycle pool

    cc.pool.removeobject (object);  

    The object you want to delete is passed in, and the object will be deleted from the Recycle pool.

  6. emptying the Recycle pool

    cc.pool.drainallpools ();  

    When you need to clear all the objects in the recycle pool, such as completing the game to go to another page, the recyclable objects in the old page are no longer useful, to avoid unnecessary memory consumption, you can use drainallpools deletes all recyclable objects.

reprint Please specify: http://www.cocos2dx.net/post/202

"COCOS2D-JS Official Documents" 12, object buffer pool December 12

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.