8 Replies Latest reply on Jan 26, 2007 11:31 AM by mihaig

    Randomly loaded .jpg's

    Social Psyence
      Hello everybody.. I know this isn't a terribly difficult thing to execute, at least conceptually, but for me with ActionScript the devil is in the details.. so anyways...

      I want to have some movie clips that will randomly load images from a specified image folder, every time the is brought to this specific frame, they would encounter a new series of images. I'm familiar with using a move clip loader, and I know there are ways to make things random within Flash.

      What I'm not sure about, however, is how to integrate the two, where if perhaps I had a folder of image001.jpg, image002.jpg, etc. that I could use something which would select these images randomly - this is still in it's conceptual stages at the moment, but any input would be appreciated greatly. Much thanks.
        • 1. Randomly loaded .jpg's
          sly one Level 1
          here's the basic drill:
          • 2. Re: Randomly loaded .jpg's
            Social Psyence Level 1
            Much thanks for the help!

            Is it considered spam here to reply to an answer with a thank you? Not 100% sure what the community guidelines are here..

            but thanks again!
            • 3. Re: Randomly loaded .jpg's
              >> my_MCL.loadClip(whichImg, imgHolder_mc);

              Sorry, perhaps I'm being thick, but I absolutely cannot get this to work.

              I made a blank movie clip at the root level, named it's instance "my_MCL", then made a blank movie clip inside that, named it's instance "imgHolder_mc", set the co-ordinates for each to 0,0 so that I could be sure it would show up on stage.

              I saved the movie and three small jpegs named "image1.jpg", "image2.jpg" and "image3.jpg" all into the same folder. I modified the script slightly This is in the first frame of the root level:



              //function to get a random mumber within a range
              function randRange(min:Number, max:Number):Number {
              var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
              return randomNum;

              randomPick = function () {
              //put random number in var, picks a number from one to ten
              var randImgNum:Number=randRange(1,3);

              //form the full image name, will result in image1.jpg, image2.jpg, etc.
              var whichImg:String = "image"+randImgNum+".jpg";

              //load image into a MovieClip with an instance name of imgHolder_mc
              my_MCL.loadClip(whichImg, imgHolder_mc);


              I attached this script to a button:

              on (release) {

              Doesn't work.

              I also tried these variations on the last line:

              this.my_MCL.loadClip(whichImg, imgHolder_mc);
              this.my_MCL.loadClip(whichImg, this.my_MCL.imgHolder_mc);
              _root.my_MCL.loadClip(whichImg, imgHolder_mc);
              _root.my_MCL.loadClip(whichImg, _root.my_MCL.imgHolder_mc);

              The trace works fine, so I know it's getting the right name. No picture loads. Ever.

              I also tried the line as originally written and named the root level movie clip to "imgHolder_mc" as you originally said (though that doesn't make sense)

              Flash 8 Pro, G4 laptop, OSX Tiger.

              • 4. Re: Randomly loaded .jpg's
                Bentmen Level 1
                Ok, I'm trying to simplify this and it's just making me nuts.

                Here's a scenario that (I think) ought to work, but doesn't:

                1) make a movie clip with three named layers ("pic", "actions", "labels")
                2) on the labels layer, make six named frames, five or ten frames apart ("a1", "a2", "a3... thru "a6")
                3) in each named frame, on the actions layer, put a stop(); action
                4) in each named frame, on the pic layer, put a different bitmap so you can see something different when you arrive at that frame.
                5) return to the root level time line, make three layers ("pic", "button", "actions"). Put a stop(); action in the actions layer
                6) put the completed movie clip of pictures on first frame of the root timeline, in the pic layer. Give it the instance name "pix"
                7) make a generic button on first frame of the root timeline, in the button layer.
                8) add this script to the button:

                on (release) {
                var myPick;
                y = random(6);
                if (y == 0) { myPick = "a1"; };
                if (y == 1) { myPick = "a2"; };
                if (y == 2) {myPick = "a3"; };
                if (y == 3) { myPick = "a4"; };
                if (y == 4) { myPick = "a5"; };
                if (y == 5) { myPick = "a6"; };
                _root.pix.goToAndPlay (myPick);

                Test the movie. Click the button. Nothing happens.

                If you trace (myPick) you can see it's operating correctly.

                If you comment out the random script and just make the button do _root.pix.gotoAndPlay("a6");, that works... Arrgh.

                What am I missing?

                • 5. Randomly loaded .jpg's
                  Bentmen Level 1
                  I've posted an example movie to show the problem here:
                  http://www.21ca.com/flash/randombug.zip (about 5k)

                  Somebody please help save what little shred of sanity I have left.

                  Many thanks.
                  • 6. Re: Randomly loaded .jpg's
                    Change your goToAndPlay to gotoAndPlay - Lower case T in goto.

                    Then it works fine.

                    • 7. Randomly loaded .jpg's
                      Bentmen Level 1

                      The curse of TTF (Typing Too Fast).

                      Also - another reason to hate case-sensitivity.
                      • 8. Re: Randomly loaded .jpg's
                        hello there, I have a better version fot you...

                        you make a mc named "hold_mc"
                        make a folder named "img" where you have your images(*.jpg)
                        use this script on your first frame:

                        j = random(i);
                        _root["hold_mc"].loadMovie ("img/" + j + ".jpg");

                        for more info: wizard5pccom@yahoo.com