2 Replies Latest reply on Jun 3, 2007 5:37 PM by Newsgroup_User

    dynamic variables

    Level 7
      I have 4 images in my library that I would like to attach to the stage
      via a for loop. I started below, with how I thought I should do this,
      but got stuck on incrementing the mcImgHolder for each one, so they each
      have a unique name. I want them to be attach called mcImgHolder1,
      mcImgHolder2, etc.

      This part is what is throwing the error for me right now:
      var ["mcImgHolder"+i]:MovieClip =
      this.createEmptyMovieClip(["mcImgHolder"+i], this.getNextHighestDepth());

      Here is what I have so far.

      var numOfImages:Number = 4;
      var spaceBetweenImg:Number = 7;
      var spaceFromTop:Number = 15;

      for(var i:Number = 0; i < numOfImages; i++){
      //Create Empty Movie clips as holders for the images
      var ["mcImgHolder"+i]:MovieClip =
      this.createEmptyMovieClip(["mcImgHolder"+i], this.getNextHighestDepth());
      this["mcImgHolder" + i]._x = 20;
      this["mcImgHolder" + i]._y = spaceFromTop;
      this["mcImgHolder" + i]._alpha = 100;

      this[mcImgHolder + i].attachMovie(["img" + i], ["img" + i],

      What am I doing wrong?

      Thanks a lot!
        • 1. Re: dynamic variables
          kglad Adobe Community Professional & MVP
          don't bother with variable typing in as2. it's not much help with debugging, does nothing else and you can't type dynamic names:

          • 2. Re: dynamic variables
            Level 7
            If you look at the createEmptyMovieClip method in Help, you'll find that it
            returns a reference to the newly created clip. So you'd want something like

            for(var i = 0; i < numOfImages; i++){
            var ref = this.createEmptyMovieClip("mcImgHolder"+i,
            ref._x = 20;
            ref._y = spaceFromTop;

            No need to specify an alpha of 100 - that's the default.

            I'm not sure what you're doing with the attachMovie next, but you're feeding
            it arrays (as you did with createEmptyMovieClip()) and it takes strings.
            This is an array: ["img" + i] If you want to use array notation to get a ref
            to a movie clip, you need to include the path like: this["img" + i] or
            picHolder["img" + i]. The brackets alone make an array.

            ref.attachMovie("img", "img" + i, depth)

            First, you had "img" + i as the first parameter... That is the linkage id of
            the clip you're attaching. It's unlikely to be a dynamic name... I put
            'depth' because you've got this.getNext... 'this' is the timeline of the
            clip you're currently 'in, so it doesn't make sense to use that as the depth
            when you're attaching in a different clip. You'd most likely want
            ref.getNextHighestDepth()) since you're attaching the img clip to ref - the
            empty clip you just created.

            You really should use the Help too, it's very good.

            Dave -
            Head Developer
            Adobe Community Expert