8 Replies Latest reply on Oct 21, 2006 2:57 PM by rfkrocktk

    Random round?

    rfkrocktk
      Hey All,
      First off, thanks to kglad for all the sweet help with sound stuff last time. Thanks dude, I owe ya one.

      Anyway, I have a question. I'm trying to set a variable (let's call it 'numberizer') to a random number, but I'm trying to set it somewhere between 1 and 10. Is there a limiter for the Math.random(); function? I'm unfamiliar with this as well as the Math.round(X); function, but here is what I have tried so far:

      var numberizer:Number = Math.random();
      var numberizer:Number = Math.round(10);

      Now, I'm assuming that I'm completely wrong on this. If anyone can help me out, that would be amazing. Hopefully it doesn't require an amazing effort to do this.

      Thanks in advance, friends :)

      - TK
        • 1. Re: Random round?
          kglad Adobe Community Professional & MVP
          if you want to generate a random integer between 1 and 10 you can use:

          • 2. Re: Random round?
            rfkrocktk Level 1
            Hey,

            Ok, it's working, almost.

            I'm basically trying to tell flash that as soon as it loads, load a random jpg into a movie clip, you know, random background type thing.
            I know that the number randomizer is working, I have tested that. However, my pictures aren't randomly loading. It worked fine when I had only two images and had a code that stated:
            quote:

            if(numberizer > 5){
            loadMovie("img1.jpg",this);
            }
            else{
            loadMovie("img2.jpg",this);
            }


            That wasn't the full code, but it WAS working.
            I have more than two images now, and this is my new code:
            quote:

            var numberizer:Number = Math.ceil(10*Math.random());
            this.onEnterFrame = function() {
            this.unloadMovie();
            if (numberizer==1) {
            loadMovie("img1.jpg", this);
            }
            if (numberizer=2) {
            loadMovie("img2.jpg", this);
            }
            if (numberizer=3) {
            loadMovie("img3.jpg", this);
            }
            if (numberizer=4) {
            loadMovie("img4.jpg", this);
            }
            if (numberizer=5) {
            loadMovie("img5.jpg", this);
            }
            if (numberizer=6) {
            loadMovie("img6.jpg", this);
            }
            };



            Seems like a lot of code, and maybe there is an easier way to do this. Either way, if anyone could help me out and help me get this working, I would very much appreciate this. I'm sure it's just some minor flaw in the coding or something.

            - TK
            • 3. Re: Random round?
              NSurveyor Level 2
              You should be using ==, the equality operator, as opposed to =, the assignment operator.

              So, in each conditional it is setting numberizer to the given value and then returns the value. As any number besides 0 is equivalent to true, it will call all of those loadMovie's.

              Also, if you are going to use that naming pattern, ie img#.jpg, you might as well as use:

              var numberizer:Number = Math.ceil(10*Math.random());
              loadMovie("img"+numberizer+".jpg", this);
              • 4. Re: Random round?
                rfkrocktk Level 1
                Awesome. That's working perfectly.

                Can anyone help me out with some xml stuff?

                I'm basically loading menu items with the xml as well as the corresponding text info.
                I'll attach my code as well as a link to the zip file containing everythinggggggg.

                Basically, I am VERY VERY NEW at XML and flash integration with it. I did a tutorial on this and just added some other functionality such as the random bg image and so forth.
                I'm trying to have each 'button' thing in my menu do a different thing: IE have two of the buttons getURL(); and one call a function which will change the background. Plus, the tooltip I am trying to do isn't working, something in my code is messed up. I know this is a ton to ask and it really sucks trying to check for code errors, but would anyone be willing to help me with this?
                I'm a visual person and if you are too, just download the zip here:
                http://asilverknightsfire.com/homepage.zip

                All of the code for XML and basic set up stuff is found in frame 1.
                All of the random bg image stuff is inside the imgholder movieclip.

                Thank you all for taking the time to help me out, I'm sure it'll all be paid back as soon as I learn this and teach others :)

                - TK
                • 5. Re: Random round?
                  NSurveyor Level 2
                  .
                  • 6. Re: Random round?
                    rfkrocktk Level 1
                    Alright! Awesome!

                    Now, how would I call the random background funtion? And do I define that function in flash or XML?
                    • 7. Re: Random round?
                      NSurveyor Level 2
                      In Flash, on Frame 1, with the rest of your code, you would put:

                      function randomF(){
                      var numberizer:Number = Math.ceil(6*Math.random());
                      supHolder.imgholder.loadMovie("img"+numberizer+".jpg");
                      }
                      • 8. Random round?
                        rfkrocktk Level 1
                        How would I make a preloader to load all of this?
                        I'm working on it but I'm supposing I have to put some of my code on the new frame one.

                        This is my preloader code:
                        stop();
                        var xml:XML = new XML();

                        this.onEnterFrame = function() {
                        var amount:Number = this.getBytesLoaded() / this.getBytesTotal() *100;
                        var amount2:Number = xml.getBytesLoaded() / xml.getBytesTotal() * 100;
                        loaderHolder.loader._yscale = amount2;
                        if(amount == 100 && amount2 ==100) {
                        this.gotoAndPlay(3);
                        delete this.onEnterFrame;
                        }
                        }

                        Also, I would love to learn how to make a preloader for each image as it is loaded due to the release state of the random button thing.
                        AND, is there any way I can fade images using the alpha setting? how would I do that? Would I call a function with an onEnterFrame state?