10 Replies Latest reply on Jan 4, 2009 11:11 AM by ronabarrett@yahoo.com

    Nested ArrayCollection

    ronabarrett@yahoo.com
      I have an ArrayCollection of 3 arrays: level0, level1, and level2. The level0 array item a set of x,y coordinates. The level1 x,y coordinates are used to position ten objects relative to the position of the level0 object. Similarly, the level2 x,y coordinates are used to position ten objects relative to the position of a level1 object.

      I wish to designate the position of an object, say an HBox, with code something like:
      <mx:HBox x="{Number(nodeCoordinates.level0[0].x)+Number(nodeCoordinates.level1[3].x)+Number(nodeCo ordinates.level2[9].x)}" />

      When I compile the code I get the followign error message:
      "1119: Access of possibly undefined property level0 through a reference with static type mx.collections:ArrayCollection."

      I know this isn't a clean way to address the problem. At this early point I'm just trying to heavy-handedly get the app to do want I want. I'll make the code more efficient later.

      How can I reference and use the values in a nested ArrayCollection?

      Thank you in advance!
        • 1. Re: Nested ArrayCollection
          rtalton Level 4
          The error comes about because you are referring to attributes of the collection's nodes, not the nodes themselves. The way you have your collection setup, you will have to use array notation:
          <mx:HBox x="{nodeCoordinates[0][0].x + nodeCoordinates[1][3].x + nodeCoordinates[2][9].x}" y="10" width="169" height="41" borderStyle="solid"/>

          You will also get compiler warnings like:
          "Data binding will not be able to detect changes when using square bracket operator. For Array, please use ArrayCollection.getItemAt() instead."

          Depending on what you are doing, you could probably use an easier method of laying out your HBoxes, and have Flex take care of all the coordinates for you, positioning items relative to another item.
          • 2. Re: Nested ArrayCollection
            ronabarrett@yahoo.com Level 1
            Thank you! Yes, your solution works as advertised, including the compiler warning. :)

            I've been trying different means of using the getItemAt method to eliminate the warnings, but have come up with the proper syntax that recognizes nested arrays in an ArrayCollection. The getItemAt method accepts an integer parameter. Does that mean I can't reference a nested array?

            Layout of components needs to be pretty specific. So, I'm not able to use the relative means for component layout.
            • 3. Re: Nested ArrayCollection
              Level 7

              "ronabarrett@yahoo.com" <webforumsuser@macromedia.com> wrote in message
              news:gjp8g6$ahg$1@forums.macromedia.com...
              > Thank you! Yes, your solution works as advertised, including the compiler
              > warning. :)
              >
              > I've been trying different means of using the getItemAt method to
              > eliminate
              > the warnings, but have come up with the proper syntax that recognizes
              > nested
              > arrays in an ArrayCollection. The getItemAt method accepts an integer
              > parameter. Does that mean I can't reference a nested array?
              >
              > Layout of components needs to be pretty specific. So, I'm not able to use
              > the
              > relative means for component layout.

              You could try something like

              x="((nodeCoordinates.getItemAt(0) as ArrayCollection).getItemAt(0) as
              YourDataType).x..."

              But I think what rtAlton is trying to say is that there's an implication
              here that you've got a "thing" at level 0 that has an x coordinate, and then
              you have another "thing" placed at level1 relative to the first thing, and
              then there's this HBox that's being positioned relative to the level1
              object.

              So it seems like you could do something that puts the level 0 and level 1
              values into a variable and adds them together, then the level 2
              ArrayCollection is pulled out as a separate variable, simplifying the
              references. Possibly this could be done with custom components that
              encapsulate each level.

              HTH;

              Amy


              • 4. Re: Nested ArrayCollection
                rtalton Level 4
                I was thinking that if one needs to place objects at fixed, non-moving positions, with the coordinates contained in an array, using binding expressions wouldn't be useful, and it would have a lot of overhead.

                I'd use nested loops, go through the 3 array collections, and use addChild to place the objects.
                • 5. Re: Nested ArrayCollection
                  ronabarrett@yahoo.com Level 1
                  Rtalton and Amy,
                  Thank you for your much appreciated assistance, which has helped me a lot to better see the problem and understand the solution.
                  Thank you very much,
                  Ron
                  • 6. Re: Nested ArrayCollection
                    rtalton Level 4
                    Is it an org chart? A process flow? UFOs? The 5th and 6th circles of Hell? We may never find out.
                    But I had to try, if only to see what all these strange x/y coordinates were doing... dun dun DUNNN....
                    To kill two birds with one stone, I put together a sample app which place objects at x/y coordinates based on an xml data file. It just uses simple loops to do this. I think this is where you could be heading with this app.
                    Strange, but TRUE: http://www.anaheimjobs.net/samples/placeobjects/
                    Here it is:
                    • 7. Re: Nested ArrayCollection
                      rtalton Level 4
                      P.S. You can right-click the app to view source, of course!
                      http://www.anaheimjobs.net/samples/placeobjects/
                      • 8. Re: Nested ArrayCollection
                        ronabarrett@yahoo.com Level 1
                        Okay, I'm impressed. You're definitely good! :)
                        Slight adjustment to your layout, however. The level3 objects should "orbit" a level 2 object, rather than the level 1 object. So, a level 3 object's coordinates would be determined as: level 1 + level 2.
                        In essence, level 3's x,y values in the array are offsets from a level 2 node's coordiates, which the array expresses as offsets from the level 1 node.
                        The layout scheme makes sense from the larger perspective of the project I'm working on. :)
                        • 9. Re: Nested ArrayCollection
                          rtalton Level 4
                          I used Level 1 as the orbit point, otherwise level 3 would be way off-screen, I think.
                          Anyway, you can see how to do this now. You just need to adjust your coordinates in the loop and you're good to go!
                          • 10. Re: Nested ArrayCollection
                            ronabarrett@yahoo.com Level 1
                            rtalton, did you see my private message to you?