1

I'm trying to build a small flash game, using AS3, where I add mobs to the stage, storing them in an Array for deletion once they've been killed. However I keep getting the following error: TypeError: Error #2007: Parameter child must be non-null.

This is my code currently:

myMobsBuilder();
var mobsArray:Array = new Array();

function myMobsBuilder():void{
    for(var i:Number=0; i<3; i++){
        this["myMob"+i+":MovieClip"] = new mob();
    lvlHolder.addChild(this["myMob"+i]);
    myMob.x = 200;
    myMob.y = 200;
    mobsArray[i] = myMobs;
    }
}

Note that I'm trying to dynamically create a variable name for each new instance of the mob. Adding the movie clip of said mob to another movie clip on stage called: lvlHolder, and positioning it. Then adding said movie clip to the Array. This is so that I can delete them both from the array and the stage once a mob has been killed. Unless there's a better way to do it of course. I've researched extensively things like: Dynamically creating variable names, adding and removing movieclips from arrays, and this is the best I can come up with, though I'm fairly new to AS3.

Any help with this would be greatly appreciated.

Ps. The following is the removal code I'm using in another function, but that isn't working so well either:

lvlHolder.removeChild(["myMob"+i]);
mobsArray.splice(i,1); 

3 Answers 3

2

It's not really clear what you are trying to do. this["myMob"+i+":MovieClip"] = new mob(); this is not valid (well, not strictly invalid, but it won't do what you think).

This should work (edit: improved code to better conform with good coding standards):

myMobsBuilder();
var mobsArray:Array = [];
//You can also use a vector here, for possibly improved speed and type safety.
//var mobsArray:Vector.<mob> = new Vector.<mob>();

function myMobsBuilder():void{
    for(var i:int=0; i<3; i++)
    {
        var tmpMob:mob = new mob();
        lvlHolder.addChild(tmpMob);
        tmpMob.x = 200;
        tmpMob.y = 200;
        mobsArray.push(tmpMob);
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

this["myMob"+i+":MovieClip"] = new mob(); <- This was in order to create a dynamic variable name so I can delete the mob() both from the array and from the stage. But I'm having problems with that too.
Can you show the code responsible for killing mobs? Once you know which mob/clip you want to kill you can reference it in several ways - no need of having name/id for each clip.
lvlHolder.removeChild(["myMob"+i]); mobsArray.splice(i,1); This is what I have so far, but it isn't working. I've been trying for days to get this to work. Ps. the removal part is in another function which activates every time a mob gets killed.
How do you find i? That's the most interesting part.
in the creation it's in the For loop. In the deletion it's in a while loop. Basically, whenever any mob is hit, a loop will go through each and every one to check their health. If their health is at or below zero, it will mark them as dead and supposedly delete them both from stage and the array. But it's not working properly :(
|
0

So, I assume your loop goes through the array?

for (var i:int = 0; i < mobsAray.length; i++)
{
    var tempMob:mob = mobsArray[i];
    var j:int;

    if (tempMob is too weak to live) //pseudocode
    {
        //now we know which one to kill

        lvHolder.removeChild(tempMob);
        //remove it from view

        j = i;
        //remember its position in the array
    }

    //you can modify the array outside the loop
    mobsArray.splice(j,1);

    //I assumed that you can kill just one mob at the time
    //but if that's not the case you can store indexes in the array
    //and make another loop to remove these from mobsArray
}

Just remember not to modify the array while looping through it.

5 Comments

Hmmm, i'll give this a try with the code above from Jonathan. Though I keep getting an error from the mobs.Array.push(tmpMob); part there.
I keep getting this from the Array.push(tmpMob); bit--- Error #1009: Cannot access a property or method of a null object reference.
Nevermind. I'm an idiot. I had the array declaration outside the function. /facepalm
Crap, it needs to be outside the function for the other function to call it. But the creation function only works if the array is inside it. weird
Glad I could help. Would you mind upvoting helpful answers and accepting the most helpful?
0

You may find it simpler/more efficient to just use a Sprite container instead of an array (depending on your needs and how you track each 'mob')

var mobContainer:Sprite = new Sprite();
lvlHolder.addChild(mobContainer);
var tmpMob:mob;
for(var i:Number=0; i<3; i++){
    tmpMob = new mob();
    mobContainer.addChild(tmpMob);
    tmpMob.x = 200;
    Mob.y = 200;
}

Then for your deletion code (if it happens within the mob class itself) just use this.parent.removeChild(this); and treat the sprite as you would the array.

So if you need to know how many mob instances there are, just use: mobContainer.numChildren (instead of array.length) and mobContainer.getChildAt(i) instead of array[i]

1 Comment

Hey, thanks but I get this error now: Parameter child must be non-null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.