DEV Community

Tu Bui
Tu Bui

Posted on

CC Pool

https://docs.cocos.com/creator/2.4/manual/en/scripting/pooling.html

onLoad: function () { this.enemyPool = new cc.NodePool(); let initCount = 5; for (let i = 0; i < initCount; ++i) { let enemy = cc.instantiate(this.enemyPrefab); // create node instance this.enemyPool.put(enemy); // populate your pool with put method } } 
Enter fullscreen mode Exit fullscreen mode
createEnemy: function (parentNode) { let enemy = null; if (this.enemyPool.size() > 0) { // use size method to check if there're nodes available in the pool enemy = this.enemyPool.get(); } else { // if not enough node in the pool, we call cc.instantiate to create node enemy = cc.instantiate(this.enemyPrefab); } enemy.parent = parentNode; // add new enemy node to the node tree enemy.getComponent('Enemy').init(); //initialize enemy } 
Enter fullscreen mode Exit fullscreen mode
onEnemyKilled: function (enemy) { // enemy should be a cc.Node instance this.enemyPool.put(enemy); // using the same put method as initializing node pool, this will also call removeFromParent for the node } 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)