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

Zac*_*ach 1 javascript phaser-framework

我有一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');
     }
}
Run Code Online (Sandbox Code Playgroud)

Jam*_*emp 5

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

clouds.forEach(function (c) { c.kill(); });
Run Code Online (Sandbox Code Playgroud)

forEach()文件.或者更好,forEachAlive().

clouds.callAll('kill');
Run Code Online (Sandbox Code Playgroud)

callAll()文件.

但是,我想知道你是否可能想要使用对象池来代替,因为我相信如果你长时间使用当前的方法,你可能会遇到垃圾收集问题.

官方编码技巧7有一些关于使用池的信息(在他们的情况下是子弹).