杀死一个组中的所有元素(Phaser)

Killing all elements in a group(Phaser)

我有一个 clouds 组会生成两朵云。每 10 秒我就想杀死那些云并再生成两朵云。你能一次杀死一个组的所有元素吗?

var clouds;
var start = new Date();
var count = 0;

function preload(){
     game.load.image('cloud', 'assets/cloud.png');
}

function create(){
     clouds = game.add.group();
}

function update(){
     if(count < 10){
         createCloud();
     }
}

function createCloud(){
     var elapsed = new Date() - start;

     if(elapsed > 10000){
       var locationCount = 0;
       //Here is where I'm pretty sure I need to
       //kill all entities in the cloud group here before I make new clouds
       while(locationCount < 2){
             //for the example let's say I have a random number
             //between 1 and 3 stored in randomNumber
             placeCloud(randomNumber);
             locationCount++;
             count++;
        }
     }
}


function placeCloud(location){
     if(location == 1){
         var cloud = clouds.create(170.5, 200, 'cloud');
     }else if(location == 2){
         var cloud = clouds.create(511.5, 200, 'cloud');
     }else{
         var cloud = clouds.create(852.5, 200, 'cloud');
     }
}

您应该能够执行以下操作之一来杀死组中的所有元素:

clouds.forEach(function (c) { c.kill(); });

forEach() documentation. Or perhaps better, forEachAlive().

clouds.callAll('kill');

callAll() documentation.

但是,我想知道您是否想考虑为此使用对象池,因为我相信如果您使用当前的方法,您可能会遇到垃圾回收问题很长一段时间。

官方 Coding Tips 7 有一些关于使用池的信息(在他们的案例中用于子弹)。