我有一个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');
}
}
发布于 2016-04-06 01:56:35
您应该能够执行以下操作之一来杀死组中的所有元素:
clouds.forEach(function (c) { c.kill(); });
文档。或者更好,forEachAlive()
。
clouds.callAll('kill');
文档。
但是,我想知道您是否希望为此使用对象池,因为我认为如果您使用当前方法很长一段时间,可能会出现垃圾收集问题。
官方的编码提示7有一些关于使用池的信息(在他们的例子中是子弹)。
https://stackoverflow.com/questions/36439565
复制相似问题