3 Replies Latest reply on Jan 2, 2009 12:03 PM by DIY_Lobotomy

    Referencing a MC...

    DIY_Lobotomy Level 1
      I have a bunch of MCs on the stage (not placed dynamically) with names like "branch123_mc", "branch395_mc", "branch412_mc", etc. Each follows the format:

      branch+ some 3-digit number+ _mc

      Those three digit numbers are all held as sub-sections called "branch" within an array called "totals"...

      Using AS2.0, I want to go through each item in the totals array, and grab that item's 3-digit branch number, then I want to reference each MC with a "onRollOver" function. The code below is what I have that isn't working (but should give you an idea of what I'm trying to do):

      for (var j:Number = 0; j < totals.length; j++) {
      var thisClip:MovieClip = ("branch" + totals[j].branch + "_mc");
      thisClip.onRollOver = function() {
      doSomething();
      }
      }

      That syntax isn't working at all. "totals[j].branch" traces out accurate numbers, so that's not the problem. Can anyone help?...
        • 1. Re: Referencing a MC...
          Level 7
          DIY_Lobotomy,

          > [this] should give you an idea of what I'm trying to do):
          >
          > for (var j:Number = 0; j < totals.length; j++) {
          > var thisClip:MovieClip = ("branch" + totals[j].branch + "_mc");
          > thisClip.onRollOver = function() {
          > doSomething();
          > }
          > }

          The trouble is that you're trying to declare a variable typed as a
          MovieClip while setting it to a string. The String class doesn't feature an
          onRollOver event, which is why this doesn't work. You'll need to reference
          the parent object of each of these movie clips. Based on your description,
          that would be the main timeline, which you can refer to with "this,"
          assuming this code appears in the main timeline.

          With that "this" reference, you have a reference to a movie clip (the
          main timeline), from which you can access that movie clip's properties. You
          can either refer to them with dot notation or with bracket notation.
          Because you want a dynamic reference, you'll have to use bracket notation:

          for (var j:Number = 0; j < totals.length; j++) {
          var thisClip:MovieClip = this["branch" + totals[j].branch + "_mc"];
          thisClip.onRollOver = function():Void {
          doSomething();
          }
          }

          Note that the brackets (array access operator, []) surround the string
          expression, and that no dot appears between "this" and the brackets.


          David Stiller
          Adobe Community Expert
          Dev blog, http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. Re: Referencing a MC...
            clbeech Level 3
            you should be able to get there more like this:
            • 3. Re: Referencing a MC...
              DIY_Lobotomy Level 1
              quote:

              Originally posted by: clbeech
              you should be able to get there more like this:


              That's perfect - thanks!!!