4 Replies Latest reply on Feb 26, 2010 5:43 AM by Ned Murphy

    ActionScript 2, attachMovie

    Zaewin

      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[i];
              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!

        • 1. Re: ActionScript 2, attachMovie
          Ned Murphy Adobe Community Professional & MVP

          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. Re: ActionScript 2, attachMovie
            ankhcomm

            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.

            • 3. Re: ActionScript 2, attachMovie
              Zaewin Level 1

              Hi!

               

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

              • 4. Re: ActionScript 2, attachMovie
                Ned Murphy Adobe Community Professional & MVP

                Your're welcome