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