【问题标题】:Action Script 3 - ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller动作脚本 3 - ArgumentError:错误 #2025:提供的 DisplayObject 必须是调用者的子对象
【发布时间】:2018-12-08 09:13:13
【问题描述】:

我正在关注http://markbennison.com/actionscript/as3-space-shooter/2-coding-projectiles/

上的 Action Script 3 教程

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

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

这是我尝试在按下空格键时发射子弹的确切代码,还有更多但不知道如何修复参数错误。


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】:

错误的意思是当这行运行时:

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,所以现在您实际上已经跳过了第二个项目符号并正在检查第三个项目,删除第一个项目符号后它现在位于索引@ 987654333@.

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

for (var i: int = bullets_arr.length - 1; i >= 0; i--) {

【讨论】:

    猜你喜欢
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-29
    • 2012-04-15
    • 1970-01-01
    相关资源
    最近更新 更多