数组中的 movieClip 显示 null,并且未显示在 stage.addChild(Array[i]) 上
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
哦,伙计,我想我有它。
你的方法很好,但我想我明白错误发生在哪里。据我所知,每次循环到一个敌人块MC时,您都会添加一个敌人块 - 然后将该敌人块MC添加到数组中(例如)5次。
因此,在enemyArray 中您将拥有 5 个对enemyBlockMC 的相同引用。
- 因此,在第二个 for 循环中的每次迭代中,您将同时拥有敌人BlockMC。
如果您打算在舞台上放置 5 个不同的敌人块,您需要执行以下操作:
这样,每次将
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:
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 toenemyArray[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
在不测试代码的情况下,我注意到你的数组,你从一个开始 - Actionscript 数组从 0 开始索引,导致你的 for 看起来是
并且
另外(只是为了理智)做:
而不是
给你更好的内存管理和开销。
看看数组计数是否修复了它 -
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
and
Additionally (just for sanity) doing:
instead of
gives you a better memory management and overhead.
See if the array counting fixes it -