2 Replies Latest reply on Jun 5, 2006 1:39 PM by Newsgroup_User

    Movie clip name as variable?

    anogarlr
      I think this should be a fairly straightforward answer, but it's eluding me. I want to assign the name of a movie clip to a variable, and then use that variable to control whatever movie clip it happens to be holding. Let me give an example:

      The script would be on frame 1:

      button1_mc.onRelease = function() {
      if(_global.selected != 'button1_mc') {
      _global.selected.gotoAndPlay ('out');
      _global.selected = 'button1_mc';
      }
      }

      button2_mc.onRelease = function() {
      if(_global.selected != 'button2_mc') {
      _global.selected.gotoAndPlay ('out');
      _global.selected = 'button2_mc';
      }
      }

      Is that even remotely feasible? I can't get it to work at all. I was hoping that I just needed a different type of variable...
        • 1. Re: Movie clip name as variable?
          Level 7
          anogarlr,

          > I want to assign the name of a movie clip to a
          > variable, and then use that variable to control
          > whatever movie clip it happens to be holding.

          There are various types of objects in Flash. There are strings,
          numbers, movie clips, dates, sounds, and a whole lot more. All you have to
          do to accomplish your goal is store a reference to the movie clip itself,
          rather than its instance name. An instance name is a string, so when you
          try to do "movie clip things" with a string, you'll find they don't work.

          The quickest way to find out how objects work is to look up their class
          entries in the ActionScript 2.0 Language Reference. Objects are defined by
          their namesake classes, which basically act like blue prints for the object.
          Movie clips are defined by the MovieClip class; dyanmic and input text
          fields are defined by the TextField class; sounds by the Sound class, and so
          on.

          > Let me give an example:
          >
          > The script would be on frame 1:
          >
          > button1_mc.onRelease = function() {
          > if(_global.selected != 'button1_mc') {
          > _global.selected.gotoAndPlay ('out');

          In this example here, you're setting the global selected variable to a
          string, "button1_mc" -- in the very next line, you're trying to invoke
          gotoAndPlay() on that string. But the String class does not provide a
          String.gotoAndPlay() method. The MovieClip class does, so the value of
          selected needs to be a movie clip.

          button1_mc.onRelease = function() {
          if (_global.selected != button1_mc) {
          // etc.
          }
          }

          Since this amended version refers directly to the object (not a quoted
          string), you're asking ActionScript to do a different thing altogether.
          Make sense?

          That said, there are times it's necessary to use a string to refer to a
          movie clip (or any other object). Sometimes, for example, you don't yet
          know the object's instance name. You may *convert* a string to an object
          reference by using the eval() function or the array access operator, as
          described here ...

          http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically


          David
          stiller (at) quip (dot) net
          Dev essays: http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. Re: Movie clip name as variable?
            blemmo Level 1
            Hi,

            _global.selected.gotoAndPlay ('out') will search for a property/MC named 'selected'. If you want to use the value of that var, you can use the array operator. To do this, you must know which MC your other MCs are attached to. Assuming it's _root, you can use the attached code. Note that there is not dot . between _root and the bracket.

            hth,
            blemmo