views:

180

answers:

2

i am new to Actionscript3, i need to know why i keep getting 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
A: 

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 -

Glycerine
yes it did, i don't get the parameter error no more. but i am still having a hard time showing my array on the stage. is my approach wrong? it seems that once i have place movieClips into the Array. i can't get them to show up one at a time.
jtdino
A: 

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 *n*th 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

Glycerine
yes everything on the stage works now. xD thanks heaps. wish i could upvote you more !
jtdino
awe no probs dude. I'm glad I could help.
Glycerine