在某种程度上,这是工作的,但它只删除tembullet对象,如果它的y位置开始于<200,如果它在舞台下的某个地方生成后到达那个点,则不是:
if(firing && bulletTimeOut == 0)
{
var tempBullet = new Bullet();
bullets.push(tempBullet);
tempBullet.x = x;
tempBullet.y = y-10;
stage.addChild(tempBullet);
trace(bullets.length);
if(tempBullet.y < 200)
{
bullets.splice(tempBullet, 1);
stage.removeChild(tempBullet);
}
bulletTimeOut = 5;
}
这段代码发生在循环中吗?
我认为,而不是仅仅检查tempBullet
的位置,你打算循环遍历整个子弹数组,以删除任何超过200px的子弹。
if(firing && bulletTimeOut == 0)
{
var tempBullet = new Bullet();
bullets.push(tempBullet);
tempBullet.x = x;
tempBullet.y = y-10;
stage.addChild(tempBullet);
trace(bullets.length);
bulletTimeOut = 5;
}
// test bullet positions whether firing or not
for each (b:Bullet in bullets) {
if(b.y < 200)
{
bullets.splice(b, 1);
stage.removeChild(b);
}
}