7 Replies Latest reply on Feb 22, 2007 12:59 AM by Peter Lorent

    Rothrock's Array shuffle sample - Flash 8 translation

    SPGAnne Level 1
      I'm sure this is easy for those who knew Actionscript in earlier versions and aren't such newbies as I with Actionscript 2. I would like to use Rothrock's old array shuffle sample code (see attached), but I'm not familiar with the old way of doing things. Do I need to write a class called: Array extends Array with a shuffle method? Should I name this new Array class something like mydomain.Array and put it in mydomain's class library folder structure? Should I make the shuffle method public static? Should I use Math.random instead of random()?

      Thanks for patient help from all you gurus out there.
        • 1. Re: Rothrock's Array shuffle sample - Flash 8 translation
          crazyjoemilan Level 2
          because flash 8 is backwards complient, you can actually just drop that bad boy on frame one of your movie, and it'll extend the Array class for you. Just make sure that thing is at the top somewhere, rather than burried.
          • 2. Re: Rothrock's Array shuffle sample - Flash 8 translation
            SPGAnne Level 1
            Thank you very much Mr mcHelpson for your willingness to help me. I'm trying to learn how to do things in the best possible Colin Moock Actionscript 2 OOD/OOP way and thus don't put any code on my movies other than to instantiate my application's main class. So I would be needing to access this shuffle method from deep inside some other class stuff. Given that, do you have any futher direction for me on how to do this in the Actionscript 2 style? If you know how, I would be much obliged if you could spell it out for me. I'm very much a newbie.
            • 3. Re: Rothrock's Array shuffle sample - Flash 8 translation
              Peter Lorent Level 2
              I believe that code is from kglad. You can extend the Array Class. Code attached.

              • 4. Re: Rothrock's Array shuffle sample - Flash 8 translation
                Level 7
                LuigiL,

                > collection.remove("David"); //no offense intended here David

                ROFL

                No offense taken. Sometimes I do get busy and fall off the face of the
                earth for a while. ;) Always glad to be back, though.


                To SPGAnne:

                >> I'm trying to learn how to do things in the best possible Colin
                >> Moock Actionscript 2 OOD/OOP way and thus don't put
                >> any code on my movies other than to instantiate my
                >> application's main class.

                Flash is a tremendously versatile, flexible tool -- and I recommend any
                of Colin Moock's books unequivocally -- at the same time, I find in my own
                work that it really is okay to use the timeline for code, even in an
                otherwise "all AS2 class" application. For example, I don't hesitate for a
                second to put a simple stop(); action among various frames in movie clip
                assets ... it's simply less hassle than targetting instances of those clips
                and checking to see when any of them has reached, say, frame 112 (or
                subclassing them and doing the same).

                So aim for OOD, for sure (I'm definitely a fan), but take care not to
                let that pursuit overshadow fundamentals -- such as understanding
                Object.prototype, the difference between random() and Math.random(), and so
                on. And that all comes with time. The learning never stops. ;)

                It really isn't necessary to subclass Array here at all (though there's
                nothing wrong with doing so). If it makes sense in your particular project
                to have an Array that can also shuffle, LuigiL's example looks like it fits
                the bill beautifully (great code!). You might, however, simply need a
                utility function/method that shuffles a plain-vanilla Array. Again,
                ActionScript is capable of all of that -- the choices are yours! -- and it's
                Choice I find even more powerful than the well-earned respect many people
                have for object-oriented design.


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


                • 5. Re: Rothrock's Array shuffle sample - Flash 8 translation
                  Peter Lorent Level 2
                  Man, did I had a good laugh! I was typing up that code, using an array with numbers and suddenly thought let's take some names from the forum. And yes, David is online!

                  Well, no, in general there is no need to subclass Array. A simple function can do the trick. But, as I said, 'you CAN extend Array'. She could probably have a Collection class that has some functions to work with arrays.

                  ....I'm still smiling here!
                  • 6. Re: Rothrock's Array shuffle sample - Flash 8 translation
                    SPGAnne Level 1
                    Again, I am so much obliged to all of you for your patient help.

                    LuigiL you are always so generous in providing me with wonderful useful code that I not only can use but always learn from. One follow on question if you don't mind.
                    In this statement:
                    var shuffled:Array=collection.shuffle();
                    I assume this means that I now have 2 arrays in memory, one called shuffled and one called collection. Is that correct? If I said:
                    collection = collection.shuffle();
                    would I have just one in memory and would this statement work?
                    • 7. Re: Rothrock's Array shuffle sample - Flash 8 translation
                      Peter Lorent Level 2
                      No, shuffled merely holds a reference to the original array 'collection' so you don't have two arrays on the stack.