23 Replies Latest reply on May 8, 2006 1:23 PM by WebXperience

    variable in a path

    Level 7
      I have a variable, called "contentp". It get set by buttons like so:
      contentp = "p1";

      I would like to use that variable in a path, but am not sure how. I was
      thinking something like the following, but get an error message.
      nav. + contentp + .enabled = false;

      The error I am getting is:
      Expected a field name after '.' operator.

      What am I doing wrong? How can I work around that?

      Thank you for any help!
      Brian
        • 1. Re: variable in a path
          Peter Lorent Level 2
          nav[contentp].enabled = false;
          • 2. Re: variable in a path
            Level 7
            Hey Luigi!
            I just want to disable one of the buttons in the nav, so I wanted to
            specify which one with the variable I already have.
            contentp = "p1";

            But the acual path to the button is
            nav.p1.enabled = false;

            Obviously there are a few of them, like p1, p2, p3, etc.

            So how can I do something like:
            nav. + contentp + .enabled = false;


            Thank you,
            Brian
            • 3. Re: variable in a path
              Level 7
              How come this does not work?
              nav.contentp.enabled = false;

              • 4. Re: variable in a path
                Bishop[Neo]
                nav.contentp.enabled is an absolute path but contentp is a variable containing the name of the actual button you want to affect. What you asked would only work if there was a button named contentp. The solution LuigiL gave works by referencing the button as an object, whose named is contained in contentp.

                Would also work if you wrote nav.["p1"].enabled = false. contentp is a container and will work no matter if you assign "p1", "p2", or "button_name".
                • 5. Re: variable in a path
                  shyaway Level 1
                  you cannot just dot a variable, flash doesn't know what you're dotting is actually the value of the variable. So, you basically refer it to .contenp and not .p1
                  as LuigiL said above, use the square brackets to evaluate a string into variable.
                  so, nav[contentp].enabled is the same thing as nav.p1.enabled

                  contentp must be a string or it won't work.
                  also, it cannot be something like
                  contentp = "p1.p1b";
                  that is not the same as nav.p1.p1b.enabled, p1b will have to be assigned to a different variable and you can dot it like this, nav[contentp][contentp2].enabled

                  got that?
                  • 6. Re: variable in a path
                    Level 7
                    Bishop[Neo]I guess I just don't get it, sorry.

                    I just tried what Luigi had suggested and it does not work for me. Maybe
                    I am doing it wrong. The button names are p1, p2, etc., they are actual
                    MovieClips rather then buttons and they are all inside nav.

                    nav..enabled = false <-- does not work for me either.

                    How do I reference a button as on object?

                    Sorry, just kind of lost.

                    Thanks so much for the help!
                    Brian

                    • 7. Re: variable in a path
                      Level 7
                      Am I missing any posts here? I only see one post Luigi did and it says
                      nothing about square brackets?

                      Thanks,
                      Brian
                      • 8. Re: variable in a path
                        Level 7
                        So how do I make contentp a string?

                        shyaway wrote:

                        > you cannot just dot a variable, flash doesn't know what you're dotting is
                        > actually the value of the variable. So, you basically refer it to .contenp and
                        > not .p1
                        > as LuigiL said above, use the square brackets to evaluate a string into
                        > variable.
                        > so, nav.enabled is the same thing as nav.p1.enabled
                        >
                        > contentp must be a string or it won't work.
                        > also, it cannot be something like
                        > contentp = "p1.p1b";
                        > that is not the same as nav.p1.p1b.enabled, p1b will have to be assigned to a
                        > different variable and you can dot it like this, nav.enabled
                        >
                        > got that?
                        >
                        • 9. Re: variable in a path
                          Bishop[Neo] Level 1
                          Okay. Assume you have 2 buttons with instance names of 'button_1' and 'button_2'. In additions you have 2 textfields with variable names of 'lefttext' and 'righttext'.

                          On button_1 you have something like:
                          on (release) {
                          _root.contentp = "lefttext";
                          }

                          and button_2 is:
                          on (release) {
                          _root.contentp = "righttext";

                          Then, in frame 1 of the Scene1 you have:
                          onEnterFrame = function(){
                          _root[contentp] = "selected";
                          }

                          When you select button_1 the variable contentp gets set to 'lefttext', the the onEnterFrame sets the textfield *named* 'lefttext' to "selected".

                          The point is, if you want to use contentp in a path as you mentioned the you have to populate it with the instance name (or in case of a textfield, the variable name) of a clip or button. If you had a movieclip named 'lefttext' you would still do _root[contentp]._visible = false; where contentp = "lefttext".

                          As Brian stated above you have to use a string to use contentp as LuigiL and I have shown. if you simple want to assign contentp the name of a clip with an instance name of "myFunkyClip' for example it would simply be:
                          contentp = myFunkyClip;

                          then you could use it like you mentioned:
                          nav.contentp.enabled = false;

                          Ultimately it depends on your usage. the brackets are used more to dynamically reference an object.

                          Better?
                          • 10. Re: variable in a path
                            shyaway Level 1
                            if you're naming your objects p1,p2,etc... that's not going to work
                            those are objects inside the library, they need to be on the stage to be considered an instance.

                            However, if you want to attach your library item to stage during run-time, give it a linkage name then you can use the my_mc.attachMovie

                            to set linkage, rightclick the mc in the library and choose "Linkage..."
                            • 11. Re: variable in a path
                              Level 7
                              Bishop[Neo], thank you for your patience with me. Not sure why I am so
                              thick in the brain on this concept but for some reason it just is not
                              working for me. Here is one more attempt to explain it.

                              I have a navigation MC with an instance name of nav.
                              I have 4 buttons inside that with the instance names of p1, p2, etc.

                              At some point I turn of all buttons like so:

                              function disableBtns(){
                              //disable the buttons
                              nav.p1.enabled = false;
                              nav.p2.enabled = false;
                              nav.p3.enabled = false;
                              nav.p4.enabled = false;
                              }

                              Then later on, I want to turn them back on, except for the one that was
                              clicked and whos section is now open.

                              I am trying to do that via so:
                              function enableBtns(){
                              //enable the buttons after the opening sequence
                              nav.p1.enabled = true;
                              nav.p2.enabled = true;
                              nav.p3.enabled = true;
                              nav.p4.enabled = true;

                              //disable the one whos section currently in
                              nav.btnDis.enabled = false;
                              }

                              The button has the following AS:
                              nav.p1.onRelease = function() {
                              contentBehaviors();
                              btnDis = p1;
                              };


                              contentBehaviors() is just a function that goes through some steps, one
                              of which is to trigger enableBtns.

                              So why does this not work?
                              Thank you so much for your help, sorry to be so lame.

                              Thank you,
                              Brian




                              • 12. Re: variable in a path
                                Bishop[Neo] Level 1
                                Posting here as well...

                                Your getting there, sort of

                                Couple of changes.
                                1. change nav.btnDis.enabled = false; to read nav[btnDis].enabled = false;

                                2. change the code in the buttons to read:
                                nav.p1.onRelease = function() {
                                btnDis = "p1";
                                contentBehaviors();
                                };


                                Note the order. I'm assuming you want to disable the button that was just clicked on, and that's what this code does. Other wise it won't disable until you click the *next* button. Also note that the button is surrounded by quotes. That means your assigning btnDis the value of "p1", a string. Then, when you call it in enableBtns() using the line I gave you above your referencing the string "p1" as an object (hence the [ ]'s ).

                                Let me know how it turns out.

                                Just for fun here is the entire AS code I used to test with. I placed 4 buttons in a clip named nav, put the nav clip on a blank stage and pasted the following AS into frame 1 of _root:

                                function disableBtns(){
                                //disable the buttons
                                nav.p1.enabled = false;
                                nav.p2.enabled = false;
                                nav.p3.enabled = false;
                                nav.p4.enabled = false;
                                }

                                function enableBtns(){
                                //enable the buttons after the opening sequence
                                nav.p1.enabled = true;
                                nav.p2.enabled = true;
                                nav.p3.enabled = true;
                                nav.p4.enabled = true;
                                nav[btnDis].enabled = false;

                                }

                                nav.p2.onRelease = function() {
                                btnDis = "p2";
                                contentBehaviors();
                                };

                                nav.p1.onRelease = function() {
                                btnDis = "p1";
                                contentBehaviors();
                                };

                                nav.p3.onRelease = function() {
                                btnDis = "p3";
                                contentBehaviors();
                                };

                                nav.p4.onRelease = function() {
                                btnDis = "p4";
                                contentBehaviors();
                                };

                                function contentBehaviors(){
                                enableBtns()
                                }
                                • 13. Re: variable in a path
                                  Level 7
                                  Luigi actually posted:

                                  nav"OPEN BRACKET"contentp"CLOSE BRACKET".enabled = false;

                                  This is the forums fault... it just doesn't like brackets... probably
                                  expects BBCode...

                                  lets see, if this works:

                                  nav[contentp].enabled =false;

                                  "Brian" <brian@agilitygraphics.com> wrote in message
                                  news:e35p6s$nvp$1@forums.macromedia.com...
                                  > Bishop[Neo]I guess I just don't get it, sorry.
                                  >
                                  > I just tried what Luigi had suggested and it does not work for me. Maybe I
                                  > am doing it wrong. The button names are p1, p2, etc., they are actual
                                  > MovieClips rather then buttons and they are all inside nav.
                                  >
                                  > nav..enabled = false <-- does not work for me either.
                                  >
                                  > How do I reference a button as on object?
                                  >
                                  > Sorry, just kind of lost.
                                  >
                                  > Thanks so much for the help!
                                  > Brian
                                  >


                                  • 14. Re: variable in a path
                                    Level 7
                                    NSurveyor, thank you so much, that actually did it, I was about to jump
                                    out the window!

                                    Thank you so much!!!

                                    Brian

                                    • 15. Re: variable in a path
                                      Bishop[Neo] Level 1
                                      No worries dude. I'm still miffed at the forums saying I just joind today (5/1) even though I've been registered with Macromedia and Adobe for 5+ years
                                      • 16. Re: variable in a path
                                        Level 7
                                        Bishop[Neo] wrote:

                                        Hey, you guys all have an e-mail like webforumsuser@macromedia.com
                                        Mine say my actual e-mail address. How can i change that? I am just
                                        using my Netscape program...

                                        Thanks again,
                                        Brian
                                        • 17. Re: variable in a path
                                          Level 7
                                          Brian wrote:
                                          > Bishop[Neo] wrote:
                                          >
                                          > Hey, you guys all have an e-mail like webforumsuser@macromedia.com
                                          > Mine say my actual e-mail address. How can i change that? I am just
                                          > using my Netscape program...
                                          >
                                          > Thanks again,
                                          > Brian

                                          When you post to the forum via the web - web forums - the Newsreaders
                                          show the email address as "webforumsuser@macromedia.com". When you use
                                          an email client or newsreader, whatever email you choose will show up.

                                          That's also where the issue of not seeing the brackets comes in... gets
                                          lost in translation btwn the web forums users and newsgroup users.
                                          • 18. Re: variable in a path
                                            Level 7
                                            You're welcome.

                                            "Brian" <brian@agilitygraphics.com> wrote in message
                                            news:e363p2$6rh$1@forums.macromedia.com...
                                            > NSurveyor, thank you so much, that actually did it, I was about to jump
                                            > out the window!
                                            >
                                            > Thank you so much!!!
                                            >
                                            > Brian
                                            >


                                            • 19. Re: variable in a path
                                              Peter Lorent Level 2
                                              Forgot about the brackets (for newsgroup readers) and that I should attach the code in that case! Sorry...
                                              • 20. Re: variable in a path
                                                nt00
                                                Scuse me if this post is in the wrong place (or the square brackets dont show up).

                                                How could I dynamically reference an object with a path that is 'deeper' than 1 _level?

                                                _level0[path_to_a_clip][variable_in_the_clip] = a_value;

                                                This is fine for
                                                path_to_a_clip=="clip_name";
                                                But isn't fine for
                                                path_to_a_clip=="clip_holding_clip_name.clip_name";

                                                I would like to use something simple like:
                                                _level0[path][variable_name]=variable_value;

                                                My work around uses path_to_a_clip.split(".") and many if,else ifs. The function is currently 30 lines due to my work around instead of the 6 that do the actual job of the function.

                                                There has to be a better way but I haven't been able to find it.

                                                Any help will be greatly appreciated. Ive been on this 1 line of actionscript for several days now.
                                                • 21. Re: variable in a path
                                                  yoohoothere
                                                  can someone help me with my actionscript
                                                  i am trying to make my space ship fly in all directions but it wont work
                                                  here is my code

                                                  onClipEvent (keyDown) {
                                                  if(Key.isDown(Key.RIGHT)) {
                                                  this._x=+x;
                                                  }
                                                  if(Key.isDown(Key.LEFT)) {
                                                  this._x=-x;
                                                  }
                                                  if(Key.isDown(Key.UP)) {
                                                  this._y=-y;
                                                  }
                                                  if(Key.isDown(Key.DOWN)) {
                                                  this._y=+y;
                                                  }
                                                  }
                                                  • 22. Re: variable in a path
                                                    WebXperience Level 1
                                                    use the eval("String expression") function which will convert all the variables within your string expression and concatenate.
                                                    example:

                                                    eval(nav. + contentp + ).enabled = false;
                                                    • 23. variable in a path
                                                      WebXperience Level 1
                                                      Make sure you are using the correct _level number. That number must be the same as the number your MC is loaded on.
                                                      _level0 is the same as the _root level or Level 0.

                                                      If you load a MovieClip on Level 5 you'd have to use:

                                                      _level5[path][variable_name]=variable_value;