4 Replies Latest reply on Jun 28, 2008 7:54 PM by system_critical55

    attachMovie(); and stage mc names

    system_critical55
      I'm attempting to create a portfolio page to house some of my movie work. I thought
      this would be a good chance to work on my understanding of XML and Flash. I created
      a XML file with three child nodes. The code is as follows:

      <?xml version="1.0" encoding="UTF-8"?>

      <projects>
      <project title="Trans/mission" link="#" thumb="" />
      <project title="Once We Were..." link="#" thumb="" />
      <project title="Televirus Glow" link="#" thumb="" />
      </projects>

      I then used attachMovie(), a for loop with childNodes[].length, and a number
      variable to make some movie clips with string + number instance names (i.e. box0, box1, box2).
      Here is the AS (in 2.0):


      // VARIABLES
      boxN = 0;
      // FUNCTIONS
      createBox = function () {
      attachMovie("projectBox","box"+boxN,boxN+1);
      boxN++;
      };
      //----
      projects = new XML();
      projects.ignoreWhite = true;
      projects.onLoad = parse;
      projects.load("xml/films.xml");
      function parse(success) {
      if (success) {
      for (i=0; i<=projects.firstChild.childNodes.length-1; i++) {
      createBox();

      }
      }
      }

      What is the best way to refer to all of these movie clips regaurdless of how long the
      childNodes[] array is? So far I can move individual boxes (ie changing ._y), but not
      more than one at a time. Thanks.

        • 1. Re: attachMovie(); and stage mc names
          clbeech Level 3
          you would then refer to the instance by the instance name you are assigning in the attachMovie method (ie: box0, box1, box2) so at any time you can access that MC by the instance name given. however you can be more efficient in the loading and attachment methods by structuring it more like this:
          • 2. Re: attachMovie(); and stage mc names
            system_critical55 Level 1
            Hi,

            Thanks for the help. This works if you have a set number of MCs and you
            know from the start how many you will have. I'm trying to build something
            more flexible. I was hoping that storing the "box"+i in a variable would work
            to position the MCs, but it does not. I'm sure there is a way to store these
            in an array and make it work, but I don't understand why a variable wouldn't
            work just as well. Something like this:

            • 3. Re: attachMovie(); and stage mc names
              clbeech Level 3
              no this is a dynamic method and will generate as many instances as there are nodes in the xml.

              the reason that the variable is not working for you, is that the way you have it declared it is a String, not a MovieClip - and you do not need to reassign the instance name of the clip to a variable. additionally, you can set the position of the clip at the time of attachment by including an Init Object reference parameter in the attachMovie method.

              also, it's never a bad idea to store the instance in an array for batch processing if needed, but you can still do so by referencing the instance of the clip as its genereated rather than using an unneeded variable reference.

              all of these items would look something more like this:
              • 4. Re: attachMovie(); and stage mc names
                system_critical55 Level 1
                Thanks again for the help. I can see how this should work. However I'm running CS3 on
                Leopard and it performs strangely. Unfortunately after I added the load for my xml file
                nothing is outputted.