6 Replies Latest reply on Jun 21, 2007 2:51 PM by Dakeyrus

    Problem with a conditional

    Dakeyrus
      First off, hello everyone. I'm kind of new to flash, but have taken a course on it this semester. Our culminating project is to develop a game based around what we've learned in the course so far. My question is this:

      I have an array for my "space" variable, and my method for redefining it is below. However, it appears that I can't use visibility as a boolean. Is there a way around this? (Whenever a button is invisible in my program, I need it to correspond with my array as such.)


      function getSpace() {
      for (x=1; x<=33; x++) {
      if (_root.P[x]._visible == false) {
      space[x] = x;
      } else if (_root.P[x]._visible == true) {
      space[x] = -x;
      }
      }
      }


      I am using an onLoad to run the function when the program starts. However, when I use a trace statement to display my array, I get an array that is undefined.

      Any help is greatly appreciated, thank you in advance.
        • 1. Re: Problem with a conditional
          kglad Adobe Community Professional & MVP
          which array is undefined? is space initialized and do you have movieclips on the _root timeline with instance names P1, P2 etc.
          • 2. Re: Problem with a conditional
            Dakeyrus Level 1
            I should have been a bit more specific, sorry about that.
            There are 33 component-based buttons with properties I've defined for the purpose of this game. This is the current code:

            _root.P17._visible = false;
            space = new Array(33);
            function getSpace() {
            for (x=1; x<=33; x++) {
            if (_root.P[x]._visible == false) {
            space[x] = x;
            } else if (_root.P[x]._visible == true) {
            space[x] = -x;
            }
            }
            }
            _root.onLoad = getSpace();

            I put in a trace statement to display the entire "space" array, but all I've gotten are "undefined, undefined, etc".

            for (x=1; x<=33; x++) {
            trace(space[x]);
            }

            Any ideas?
            • 3. Re: Problem with a conditional
              Greg Dove Level 4
              Based on the example:
              _root.P17._visible = false;

              I'm guessing you'll get it working by changing:

              _root.P[x]

              to
              _root["P"+x]

              wherever it appears
              • 4. Re: Problem with a conditional
                Dakeyrus Level 1
                That did it! Thanks a ton.

                Is there any chance you could explain why it works like that so I don't make the same mistake again?
                • 5. Re: Problem with a conditional
                  Greg Dove Level 4
                  I don't actually know the official terminology... kglad or others could probably explain it using the correct terms... but here's how I understand it:

                  if you have an object hierachy (movieclips or other objects)
                  then you can access the child objects using dot notation:

                  e.g.
                  _root.Parentclip1.childclip1.childclip1._x

                  which is fine for static access to the _x property of the nested clip

                  but if you want to access using 'dynamic' names then you need to enclose the string names in the array access square braces.

                  _root["Parentclip1"]["childclip1"]["childclip1"]._x

                  will acheive the same thing as above. Of course its more interesting and useful if you're creating the strings - e.g. with a loop in the same way that you were doing.

                  I think this is like (or the same as?) associative array access.
                  • 6. Re: Problem with a conditional
                    Dakeyrus Level 1
                    Gotcha, pretty sure I understand what you're saying. Thanks a ton! Game is almost done now :)