2016-04-06 2 views
0

私はcloudsグループを持っており、2つの雲を生成します。 10秒ごとに私はそれらの雲を殺し、さらに2つの雲を産みたい。一度にグループのすべての要素を殺すことはできますか?グループ内のすべての要素を殺す(Phaser)

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

答えて

3

あなたは、グループ内のすべての要素を殺すために、次のいずれかを行うことができるはず:

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

forEach() documentation。またはおそらく、より良い、forEachAlive()

clouds.callAll('kill'); 

しかし、私はと考えてと考えているので、現在のメソッドを長時間使用するとガベージコレクションの問題が発生する可能性があります。

公式Coding Tips 7には、プールの使用に関する情報(その場合は箇条書き)があります。

関連する問題