2 Replies Latest reply on Nov 20, 2006 7:58 AM by Cetrez

    Using variables in paths to objects

    Cetrez
      Hi

      I'm wondering how I can use a string in a declared variable for a path to an object. Here's an example:

      On the press of a button a variable is declared:

      _root.nextsection = "news";



      And later on I want to use the string "news" in a path, like this (on a frame action):

      if (_root.nextsection == "news") {
      this.nextsection.gotoAndPlay(1);

      }

      As you can see in the second line I'm using the variable name as a path... but this doesn't work.

      Any suggestions?
      Thanks
        • 1. Re: Using variables in paths to objects
          Level 7
          Depending of where your if statement is, if your this.nextsection is the
          same as _root.nextsection, since this is a string and not an mc, what it
          does is:

          "news".gotoAndPlay(1).

          If you had a movieclip named news in the _root, then you could do:

          _root[_root.nextsection].gotoAndPlay(1) // EQUIVALENT TO _root["news"] or
          _root.news

          Other option is:
          var mynextsection_mc = eval("_root." + _root.nextsection) ;
          mynextsection_mc.gotoAndPlay(1);

          Flash will evaluate your constructed string "_root.news" to an object and
          return it, if it doesn't match anything, then you will get an undefined.

          JG


          "Cetrez" <webforumsuser@macromedia.com> wrote in message
          news:ejqacs$q6r$1@forums.macromedia.com...
          > Hi
          >
          > I'm wondering how I can use a string in a declared variable for a path to
          > an
          > object. Here's an example:
          >
          > On the press of a button a variable is declared:
          >
          > _root.nextsection = "news";
          >
          >
          >
          > And later on I want to use the string "news" in a path, like this (on a
          > frame
          > action):
          >
          > if (_root.nextsection == "news") {
          > this.nextsection.gotoAndPlay(1);
          >
          > }
          >
          > As you can see in the second line I'm using the variable name as a path...
          > but
          > this doesn't work.
          >
          > Any suggestions?
          > Thanks
          >


          • 2. Re: Using variables in paths to objects
            Cetrez Level 1
            Ok, I got it. :)
            Thanks