6 Replies Latest reply on Nov 17, 2007 3:19 PM by Greg Dove

    Dynamically named properties

    gvbrown49686
      This should be simple to do, but I'm not finding an answer. Here's what I'm up to...

      I'd like to adjust the property of an object, but I want the property to be assigned dynamically. For example:

      public var scrollAxis="y";

      and then later:

      spriteObject.scrollAxis= spriteObject.scrollAxis+30;

      Where, scrollAxis is the "y" property of the spriteObject. Granted, I could do a case statement based on the value of "scrollAxis" (x or y), but this is accessed too many times in the code to be efficient. Something like an evaluate() function or something... though I haven't found that it exists?

      Thanks -
        • 1. Dynamically named properties
          Greg Dove Level 4
          maybe:

          spriteObject[scrollAxis] = spriteObject[scrollAxis] +30;
          or
          spriteObject[scrollAxis] +=30;
          • 2. Re: Dynamically named properties
            clbeech Level 3
            yeah GWD (hey), the second for sure.

            But also gv, it looks like your assigning the property 'scrollAxis' as a String in the class def as above, and it should be a Number if you're storing the y value of the object, and you may want to use something like:

            spriteObject.y +=30;
            spriteObject['scrollAxis'] = spriteObject.y;

            additionally, using this method the property would not need to be declared within the class file, the access operator, is assigning the new property.
            • 3. Re: Dynamically named properties
              Greg Dove Level 4
              hey cbeech!

              I think gv wants scrollAxis to be a string. Sometimes it could be "y", sometimes "x" (do we have a "z" yet?)

              So a function for scrolling might change the scrolling to be vertical or horizontal. Just a guess.
              • 4. Re: Dynamically named properties
                clbeech Level 3
                hmmm, that's an interesting thought, I wonder if you can call on a static property as a string like:

                movieclip['_y'] = 100;

                a little offtopic ( sorry there, gv) but interesting, I'll have to try that.

                (@GWD: OT - there's been a movement to establish a DB system that will contain standard question answers for quick referencing by those of us here that are regulars, dzedward has cooridinated a php driven back end system and DB where we'll start to compile data, you may want to get in on this, it should be handy resource tool for us without having to retype answers that are common. there's even a new domain www.flashgods.org :) check out the post in the General discussion section, it's pretty long though 49-50 or so, and it's probably a couple of page deep now, as discussion has cooled off a little for now.)

                And kglad - if you're listening, your input would be invaluable as well. since you know damn near everything :)
                • 5. Re: Dynamically named properties
                  gvbrown49686 Level 1
                  GWD had it... here's how you do it.

                  With cs3, you now use this, with no quotes on scrollAxis:

                  spriteObject[scrollAxis]

                  scrollAxis is either x or y, and would be used to set or get the x or y value of the spriteObject.

                  • 6. Re: Dynamically named properties
                    Greg Dove Level 4
                    @cbeech: for movieclip['_y'] = 100; you certainly can do it that way.
                    re OT:I'll take a look at that site on Monday. I think it could save time. But Adobe should really do something like that here, they could harvest a lot of common Q&As from the forums. FAQs that you arrive at from multiple taxonomy/category terms.
                    Never mind, I know they're busy doing other stuff like flash 10. I'll check it out Monday, its not working at the moment.