CC Pool

CC Pool

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

onLoad: perform () {
    this.enemyPool = new cc.NodePool();
    let initCount = 5;
    for (let i = 0; i < initCount; ++i) {
        let enemy = cc.instantiate(this.enemyPrefab); // create node occasion
        this.enemyPool.put(enemy); // populate your pool with put methodology
    }
}
createEnemy: perform (guardianNode) {
    let enemy = null;
    if (this.enemyPool.dimension() > 0) { // use dimension methodology to verify if there're nodes accessible within the pool
        enemy = this.enemyPool.get();
    } else { // if not sufficient node within the pool, we name cc.instantiate to create node
        enemy = cc.instantiate(this.enemyPrefab);
    }
    enemy.guardian = guardianNode; // add new enemy node to the node tree
    enemy.getComponent('Enemy').init(); //initialize enemy
}
onEnemyKilled: perform (enemy) {
    // enemy ought to be a cc.Node occasion
    this.enemyPool.put(enemy); // utilizing the identical put methodology as initializing node pool, this may also name removeFromParent for the node
}

Vinyl

through DEV Community https://dev.to

March 14, 2025 at 08:55AM