Skip to main content
February 26, 2010
Answered

ActionScript 2, attachMovie

  • February 26, 2010
  • 2 replies
  • 4275 views

Hey, people!

I've got a small issue with attachMovie.

I've got an array with references to movie clips which I want to place at random locations on the scene.

Here is my function:

function randomize(target_array){
    top_y = 87;
    bottom_y = 296;
   
    left_x_range = 16;
    right_x_range = 220;
   
    left_x_range_second = 305;
    right_x_range_second = 535;
   
    for(i = 0; i < target_array.length; i++){
        var mc = target_array;
        trace(mc);
       
        mc_clothes_holder.attachMovie(mc, mc, mc_clothes_holder.getNextHighestDepth());
       
        currY = Math.floor(Math.random() * (1 + bottom_y - top_y)) + top_y;
       
        if(i%2 != 0){
            currX = Math.floor(Math.random() * (1 + right_x_range - left_x_range)) + left_x_range;
            mc_clothes_holder.mc._x = currX;
            mc_clothes_holder.mc._y = currY;           
        }else{
            currX = Math.floor(Math.random() * (1 + right_x_range_second - left_x_range_second)) + left_x_range_second;
            mc_clothes_holder.mc._x = currX;
            mc_clothes_holder.mc._y = currY;
           
            trace(mc_clothes_holder.mc);
        }
    }
}

The thing here is that the trace of "mc" in the beginning returns the correct movie clip reference, but "mc_clothes_holder.mc" after the attachMovie returns "undefined".

Does anyone have any idea why this doesn't work? What have I done wrong?

Thanks!

This topic has been closed for replies.
Correct answer Ned Murphy

Your problem may lie in the way you are using "mc"  In an attachMovie scenario, the first argument is supposed to be the linkage ID of the object in the library.  Then you go and name the instance you create that same thing, "mc", which is bound to conflict with the var mc you already defined which is merely a string value.

I'm tempted to tell you to try tracing...

trace(mc_clothes_holder[mc]);

because if mc is a string, and is the instance name as well, you would need to use bracket notation (or eval) to have the string treated as an object reference.

But I'd recommend changing the name of the instance you create to something else, maybe: mc+"Obj" just to avoid any confusion...

trace(mc_clothes_holder[mc+"Obj"]);

2 replies

Known Participant
February 26, 2010

Try

 mc_clothes_holder.attachMovie("mc", "mc", mc_clothes_holder.getNextHighestDepth());

I know you probably mean mc is a string and it's already within quotes in your file, but I put them in there just in case that's the issue you're having.

Personally though, I always do something like

 mc_clothes_holder.attachMovie("mc", "mcOnStage", mc_clothes_holder.getNextHighestDepth());

I don't know if repeating the same name is your issue; I've always done it like the way above, thus I've never bothered to check if it works or not by repeating mc, mc as you do.

Ned Murphy
Ned MurphyCorrect answer
Legend
February 26, 2010

Your problem may lie in the way you are using "mc"  In an attachMovie scenario, the first argument is supposed to be the linkage ID of the object in the library.  Then you go and name the instance you create that same thing, "mc", which is bound to conflict with the var mc you already defined which is merely a string value.

I'm tempted to tell you to try tracing...

trace(mc_clothes_holder[mc]);

because if mc is a string, and is the instance name as well, you would need to use bracket notation (or eval) to have the string treated as an object reference.

But I'd recommend changing the name of the instance you create to something else, maybe: mc+"Obj" just to avoid any confusion...

trace(mc_clothes_holder[mc+"Obj"]);

February 26, 2010

Hi!

Thanks for your help. This was exactly what was wrong and my movieclips are now working as they should

Ned Murphy
Legend
February 26, 2010

Your're welcome