操作脚本3 - ArgumentError:错误#2025:提供的DisplayObject必须是调用者的子级

问题描述 投票:1回答:1

我在http://markbennison.com/actionscript/as3-space-shooter/2-coding-projectiles/上关注Action Script 3的本教程

我在第2部分编码射弹我不知道为什么它在我按下播放时总是说错误

“ArgumentError:错误#2025:提供的DisplayObject必须是调用者的子节点。”

这是我试图通过按空格键时射击子弹的确切代码,还有更多但是idk如何修复参数错误。


function addBullet(startX,startY):void {

//declare an object instance from the bullet Class
var b: Bullet = new Bullet();

//set the new bullet's x-y coordinates
b.x = startX;
b.y = startY;

//add the new bullet to the stage
stage.addChild(b);

//store the object in an array
bullets_arr.push(b);

}

moveBullet函数():无效{

//loop through all instances of the bullet

//loop from '0' to 'number_of_bullets'
for (var i: int = 0; i < bullets_arr.length; i++) {
    //move the bullet
    bullets_arr[i].x += bulletSpeed;

    //if the bullet flies off the screen, remove it
    if (bullets_arr[i].x > stage.stageWidth) {
        //remove the bullet from the stage
        stage.removeChild(bullets_arr[i]);

        //remove the bullet from the array
        bullets_arr.removeAt(i);
    }
}

}


有人可以给我改变任何东西的提示吗?

javascript actionscript-3 flash actionscript flash-cs6
1个回答
2
投票

该错误表示此行运行时:

stage.removeChild(bullets_arr[i]);

bullets_arr[i]引用的项目实际上并不在舞台上。可能因为它已经从舞台上移除了。

虽然这可能不是您的错误的确切原因,但这里的一个重要问题是从您当前正在迭代的数组中删除项目。

当你这样做:bullets_arr.removeAt(i);时,你正在改变bullets_arr数组的大小。

在第一次迭代中,i0。你的第一个子弹是bullets_arr[0],你的第二个子弹是bullets_arr[1],你的第三个是bullets_arr[2]等。如果在第一个循环中,你最终从阵列中移除了项目,这意味着指数已经移位,所以现在你的第二个子弹是bullets_arr[0],但是在下一个循环i增加到1,所以现在你实际上跳过了第二个子弹并检查第三个项目,在删除第一个项目后现在位于索引1

在你的moveBullet函数中,更改循环使其向后迭代,这样如果你删除一个项目,它不会移动你还没有迭代的索引。

for (var i: int = bullets_arr.length - 1; i >= 0; i--) {
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.