数组中的 movieClip 显示 null,并且未显示在 stage.addChild(Array[i]) 上

发布于 2024-08-30 11:46:42 字数 1427 浏览 12 评论 0原文

我是 Actionscript3 的新手,我需要知道为什么我不断收到错误Parameter child must be non-null。我的代码不会在舞台上显示 5 个 enemyBlock 对象,而只会显示一个。 任何提示和帮助将不胜感激。提前致谢。

返回:

 TypeError: Error #2007: Parameter child must be non-null.
    at flash.display::DisplayObjectContainer/addChild()
    at flash.display::Stage/addChild()
    at BlockDrop_fla::MainTimeline/EnemyBlockPos()
    at BlockDrop_fla::MainTimeline/frame2()


// declare varibles 
var isEnemyMoving:Boolean = false; 
var enemyArray:Array; 
var enemyBlock:MovieClip = new EnemyBlock(); // assign EnemyBlock class to enemyBlock
var enemyBlockMC:MovieClip; 

var count:int = 5;

var mapWidth:Number = 800;
var mapHeight:Number = 600;

function EnemyBlockPos() :void {

    // assign new MovieClip not null
    enemyBlockMC = new MovieClip;
    enemyArray = new Array();

        for(var i=1; i<= count; i++){ 
            // add class to MC
            enemyBlockMC.addChild(enemyBlock);
            // randomize position
            enemyBlock.x = Math.round(Math.random()*mapWidth);
            enemyBlock.y = Math.round(Math.random()*mapHeight);
            // set motion
            enemyBlock.movement = 5;

            // add MC to array
            enemyArray.push(enemyBlockMC);
        }


        for (var w = 1; w <= enemyArray.length; w++) {
                addChild(enemyArray[w]);
            }

} // endOf EnemyBlockPos

I am new to Actionscript3, I need to know why I keep getting the error Parameter child must be non-null. And my code won't display 5 enemyBlock objects onto the stage but only just one.
any tips and help will be much appreciated. thanks in advance.

Returns:

 TypeError: Error #2007: Parameter child must be non-null.
    at flash.display::DisplayObjectContainer/addChild()
    at flash.display::Stage/addChild()
    at BlockDrop_fla::MainTimeline/EnemyBlockPos()
    at BlockDrop_fla::MainTimeline/frame2()


// declare varibles 
var isEnemyMoving:Boolean = false; 
var enemyArray:Array; 
var enemyBlock:MovieClip = new EnemyBlock(); // assign EnemyBlock class to enemyBlock
var enemyBlockMC:MovieClip; 

var count:int = 5;

var mapWidth:Number = 800;
var mapHeight:Number = 600;

function EnemyBlockPos() :void {

    // assign new MovieClip not null
    enemyBlockMC = new MovieClip;
    enemyArray = new Array();

        for(var i=1; i<= count; i++){ 
            // add class to MC
            enemyBlockMC.addChild(enemyBlock);
            // randomize position
            enemyBlock.x = Math.round(Math.random()*mapWidth);
            enemyBlock.y = Math.round(Math.random()*mapHeight);
            // set motion
            enemyBlock.movement = 5;

            // add MC to array
            enemyArray.push(enemyBlockMC);
        }


        for (var w = 1; w <= enemyArray.length; w++) {
                addChild(enemyArray[w]);
            }

} // endOf EnemyBlockPos

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦明 2024-09-06 11:46:42

哦,伙计,我想我有它。

你的方法很好,但我想我明白错误发生在哪里。据我所知,每次循环到一个敌人块MC时,您都会添加一个敌人块 - 然后将该敌人块MC添加到数组中(例如)5次。

因此,在enemyArray 中您将拥有 5 个对enemyBlockMC 的相同引用。
- 因此,在第二个 for 循环中的每次迭代中,您将同时拥有敌人BlockMC。

如果您打算在舞台上放置 5 个不同的敌人块,您需要执行以下操作:

   for(var i:int =0; i<= count - 1; i++){ 
            // add class to MC
/*
Move this line of code into the for loop, creating a new version every time.
*/
enemyBlockMC = new MovieClip;
/*
Also move this into your loop, ensuring you make a new EnemyBlock() every time
*/
var enemyBlock:MovieClip = new EnemyBlock(); // assign EnemyBlock class to enemyBlock
enemyBlockMC.addChild(enemyBlock);
// randomize position
enemyBlock.x = Math.round(Math.random()*mapWidth);
enemyBlock.y = Math.round(Math.random()*mapHeight);
// set motion
enemyBlock.movement = 5;

// add MC to array
enemyArray.push(enemyBlockMC);
}

这样,每次将 enemyBlockMC 推入您的敌人数组时,都会有一个新版本的敌人块包裹在影片剪辑中。

话虽如此,您将拥有第 n 个敌人块,其中全部都是新版本。因此,当您在第二个 for 循环中 addChild(enemyArray[w]); 时,每次都会有一个新版本。

本质上(为了澄清) enemyArray[0] 是一个与 enemyArray[2] 完全不同的对象

希望它有意义。 - 如果你需要我再解释一遍,尽管问。

这就是你的目的吗?
对代码格式感到抱歉——o_O

Ooh dude I think I have it.

Your approach is fine but I think I see where the error occurs. As far as I can see, you add an enemyBlock each time you loop to the one enemyBlockMC - Then you add that enemyBlockMC to the array (e.g.) 5 times.

therefore you'll have the 5 same referances to the enemyBlockMC in enemyArray.
- So you'll have enemyBlockMC the same time each itteration in your second for loop.

If you intended to have 5 different enemyBlock's on the stage you need to do something like this:

   for(var i:int =0; i<= count - 1; i++){ 
            // add class to MC
/*
Move this line of code into the for loop, creating a new version every time.
*/
enemyBlockMC = new MovieClip;
/*
Also move this into your loop, ensuring you make a new EnemyBlock() every time
*/
var enemyBlock:MovieClip = new EnemyBlock(); // assign EnemyBlock class to enemyBlock
enemyBlockMC.addChild(enemyBlock);
// randomize position
enemyBlock.x = Math.round(Math.random()*mapWidth);
enemyBlock.y = Math.round(Math.random()*mapHeight);
// set motion
enemyBlock.movement = 5;

// add MC to array
enemyArray.push(enemyBlockMC);
}

That way, every time you push enemyBlockMC into your enemyArray, is a new version of enemyBlock wrapped inside a movieclip.

With that said, you'll have nth number of enemyBlocks of which are all new versions. Therefore when you addChild(enemyArray[w]); in your second for loop, you'll have a new version every time.

In essence (to clarify) enemyArray[0] is an entirely different object to enemyArray[2]

Hope it makes sense. - If you need me to explain it again, just ask.

Is that what your were going for?
Sorry about the code formatting -- o_O

や莫失莫忘 2024-09-06 11:46:42

在不测试代码的情况下,我注意到你的数组,你从一个开始 - Actionscript 数组从 0 开始索引,导致你的 for 看起来是

for(var i:int = 0; i<= count - 1; i++){ 
        // add class to MC
...

并且

for (var w:int = 0; w <= enemyArray.length -1; w++) {
...

另外(只是为了理智)做:

enemyArray = []

而不是

enemyArray = new Array();

给你更好的内存管理和开销。

看看数组计数是否修复了它 -

Without testing the code I notice your array, you start at one - Actionscript array are index'd from 0, resulting in your for look to be

for(var i:int = 0; i<= count - 1; i++){ 
        // add class to MC
...

and

for (var w:int = 0; w <= enemyArray.length -1; w++) {
...

Additionally (just for sanity) doing:

enemyArray = []

instead of

enemyArray = new Array();

gives you a better memory management and overhead.

See if the array counting fixes it -

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文