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

时间:2016-04-06 00:50:09

标签: 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');
     }
}

1 个答案:

答案 0 :(得分:5)

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

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

forEach() documentation。或者更好,forEachAlive()

clouds.callAll('kill');

callAll() documentation

但是,我想知道您是否可能想要使用对象池,因为我相信如果您长时间使用当前方法可能会出现垃圾回收问题时间。

官方Coding Tips 7有一些关于使用游泳池的信息(在他们的情况下是子弹)。