6 Replies Latest reply on May 4, 2007 5:24 AM by Newsgroup_User

    moving variables into a command?

    Level 7
      Hi,

      I could probably find this answer by searching but I do not think I am using
      the correct terminology. I hope this is clear enough for some help.

      I have set a variable in a movie called myVar

      I concoct it on another level and trace it:

      trace("_parent._parent." + myVar +"nextFrame());

      - and it outputs the way I want it to. I do not know how to make the command
      work though. I have tried:
      call("_parent._parent." + myVar +"nextFrame());
      eval("_parent._parent." + myVar +"nextFrame());
      ["_parent._parent." + myVar +"nextFrame()];

      Can anyone tell me how to make the command function?

      Thanks in advance,
      Doug


        • 1. Re: moving variables into a command?
          sly one Level 1
          Assuming that myVar is a reference to a MovieClip, shouldn't it be:

          _parent._parent[myVar].nextFrame();
          • 2. Re: moving variables into a command?
            Level 7
            Thanks for the reply Sly One. The myVar is in reference to a MovieClip but I
            tried what you suggested and it did not work.

            Any other ideas?

            Thanks again,
            Doug


            "sly one" <webforumsuser@macromedia.com> wrote in message
            news:f1e470$buq$1@forums.macromedia.com...
            > Assuming that myVar is a reference to a MovieClip, shouldn't it be:
            >
            > _parent._parent[myVar].nextFrame();


            • 3. Re: moving variables into a command?
              Level 7
              Douglas,

              > Thanks for the reply Sly One. The myVar is in reference to
              > a MovieClip but I tried what you suggested and it did not
              > work.

              The "trick" here is to get a MovieClip instance out of that myVar
              variable. You said you have a myVar variable, but you haven't told us what
              it's value is -- not exactly. You said it's "in reference" to a movie clip
              ... but is it an actual movie clip object reference, or is it a string that
              represents that clip's instance name?

              If it's an object reference, you don't need anything special, such as
              eval() or the array access operator ([...]) -- it's a reference, and is
              therefore the same as using the clip's actual instance name. If it's a
              string, that's when you need to convert it. The array access operator
              converts strings into object properties, so you'll need a reference to
              whatever object *contains* the clip in question. I'm going to make up an
              instance name, myClip. If, under normal circumstances, myClip is here ...

              clipA.clipB.myClip

              ... then an idential array access operator approach might be this:

              clipA.clipB["myClip"]

              ... or this ...

              clipA.clipB["my" + "Clip"]

              ... etc.

              You just drop the dot that normally precedes the instance name, then
              wrap the instance name in quotation makes and pop it between square
              brackets. A variable that represents the same string would do just as well:

              var str:String = "myClip";
              clipA.clipB[str]



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


              • 4. Re: moving variables into a command?
                Level 7
                Hi David,

                Thank you for your eloquent reply. I appreciate how understandable it was. I
                think that I have attempted it correctly but it still does not seem to work.
                I have a workaround within the following code that is commented out that
                might give you extra information. The commented out code works fine. I would
                still like to have this figured out for cleaner code and future reference.
                This is attached to a button. The myVar variable is actually called letsGo.
                Take a look:

                on (press) {
                //if (_parent.letsGo == "demos1") {
                //_parent._parent.demos.demos1.nextFrame();
                //} else if (_parent.letsGo == "demos2") {
                //_parent._parent.demos.demos2.nextFrame();
                //} else if (_parent.letsGo == "demos3") {
                //_parent._parent.demos.demos3.nextFrame();
                //}
                _parent._parent["demos." + letsGo].nextFrame();
                }

                Thanks in advance for your help,
                Doug


                "David Stiller" <stiller@quip-remove-.net> wrote in message
                news:f1e85j$gal$1@forums.macromedia.com...
                > Douglas,
                >
                >> Thanks for the reply Sly One. The myVar is in reference to
                >> a MovieClip but I tried what you suggested and it did not
                >> work.
                >
                > The "trick" here is to get a MovieClip instance out of that myVar
                > variable. You said you have a myVar variable, but you haven't told us
                > what it's value is -- not exactly. You said it's "in reference" to a
                > movie clip ... but is it an actual movie clip object reference, or is it a
                > string that represents that clip's instance name?
                >
                > If it's an object reference, you don't need anything special, such as
                > eval() or the array access operator ([...]) -- it's a reference, and is
                > therefore the same as using the clip's actual instance name. If it's a
                > string, that's when you need to convert it. The array access operator
                > converts strings into object properties, so you'll need a reference to
                > whatever object *contains* the clip in question. I'm going to make up an
                > instance name, myClip. If, under normal circumstances, myClip is here ...
                >
                > clipA.clipB.myClip
                >
                > ... then an idential array access operator approach might be this:
                >
                > clipA.clipB["myClip"]
                >
                > ... or this ...
                >
                > clipA.clipB["my" + "Clip"]
                >
                > ... etc.
                >
                > You just drop the dot that normally precedes the instance name, then
                > wrap the instance name in quotation makes and pop it between square
                > brackets. A variable that represents the same string would do just as
                > well:
                >
                > var str:String = "myClip";
                > clipA.clipB[str]
                >
                >
                >
                > David Stiller
                > Adobe Community Expert
                > Dev blog, http://www.quip.net/blog/
                > "Luck is the residue of good design."
                >


                • 5. Re: moving variables into a command?
                  Level 7
                  Doug,

                  > The commented out code works fine.

                  Okay.

                  > I would still like to have this figured out for cleaner code
                  > and future reference.

                  Sure, I can appreciate that. :)

                  > This is attached to a button. The myVar variable is actually
                  > called letsGo.
                  > Take a look:
                  >
                  > on (press) {
                  > //if (_parent.letsGo == "demos1") {

                  Okay, this tells me that the value of letsGo is a string. How do I know
                  that? Because a) you said the commented part works as expected, and b)
                  you're comparing letsGo to a string ... in this case, "demos1". Note, also,
                  that the full path to this variable -- from the point of vew of this
                  button -- is _parent.letsGo.

                  > //_parent._parent.demos.demos1.nextFrame();

                  Good. Now I know the other key point; namely, the path to a set of
                  movie clips whose instance names are demos1, demos2, etc.

                  > //} else if (_parent.letsGo == "demos2") {
                  > //_parent._parent.demos.demos2.nextFrame();
                  > //} else if (_parent.letsGo == "demos3") {
                  > //_parent._parent.demos.demos3.nextFrame();
                  > //}

                  So, yes, to avoid all the if() statements, you need to combined two
                  concepts: a) the path to the movie clip(s) with b) the path to the letsGo
                  variable. Part A seems to be this:

                  _parent._parent.demos. INSTANCE NAME .nextFrame();

                  ... and we already know that part B is _parent.letsGo. So we need to swap
                  the stand-in INSTANCE NAME area with the expression _parent.letsGo, wrapped
                  in the array access operator, because that's what converts the string:

                  _parent._parent.demos[_parent.letsGo].nextFrame();


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


                  • 6. Re: moving variables into a command?
                    Level 7
                    David,

                    Thank you so much for your patience. Not only is the problem solved, but I
                    have also expanded my Flash Actionscript vocabulary (array access operator -
                    awesome!). Very Helpful.

                    Thanks again,
                    Doug


                    "David Stiller" <stiller@quip-remove-.net> wrote in message
                    news:f1f82d$lsc$1@forums.macromedia.com...
                    > Doug,
                    >
                    >> The commented out code works fine.
                    >
                    > Okay.
                    >
                    >> I would still like to have this figured out for cleaner code
                    >> and future reference.
                    >
                    > Sure, I can appreciate that. :)
                    >
                    >> This is attached to a button. The myVar variable is actually
                    >> called letsGo.
                    >> Take a look:
                    >>
                    >> on (press) {
                    >> //if (_parent.letsGo == "demos1") {
                    >
                    > Okay, this tells me that the value of letsGo is a string. How do I
                    > know that? Because a) you said the commented part works as expected, and
                    > b) you're comparing letsGo to a string ... in this case, "demos1". Note,
                    > also, that the full path to this variable -- from the point of vew of this
                    > button -- is _parent.letsGo.
                    >
                    >> //_parent._parent.demos.demos1.nextFrame();
                    >
                    > Good. Now I know the other key point; namely, the path to a set of
                    > movie clips whose instance names are demos1, demos2, etc.
                    >
                    >> //} else if (_parent.letsGo == "demos2") {
                    >> //_parent._parent.demos.demos2.nextFrame();
                    >> //} else if (_parent.letsGo == "demos3") {
                    >> //_parent._parent.demos.demos3.nextFrame();
                    >> //}
                    >
                    > So, yes, to avoid all the if() statements, you need to combined two
                    > concepts: a) the path to the movie clip(s) with b) the path to the letsGo
                    > variable. Part A seems to be this:
                    >
                    > _parent._parent.demos. INSTANCE NAME .nextFrame();
                    >
                    > ... and we already know that part B is _parent.letsGo. So we need to swap
                    > the stand-in INSTANCE NAME area with the expression _parent.letsGo,
                    > wrapped in the array access operator, because that's what converts the
                    > string:
                    >
                    > _parent._parent.demos[_parent.letsGo].nextFrame();
                    >
                    >
                    > David Stiller
                    > Adobe Community Expert
                    > Dev blog, http://www.quip.net/blog/
                    > "Luck is the residue of good design."
                    >