4 Replies Latest reply on Feb 10, 2017 1:15 PM by kglad

    Stop math.random generating numbers

    jackh8500410 Level 1

      Hi,

       

      I have made a test which has a "test over" page at the end. When the test has finished and goes to this page, I cannot get the buttons to work - even though it was using a code snippet. I have noticed that math.random() keeps generating numbers when I get to the "test over" page and wondered if this was the reason why I could not get any buttons to work.

      Is there a way to stop math.random() from generating numbers? The code for it is;

       

      var MeSelRam: Number = Math.random();

      MeSelectQu = Math.round(MeSelRam * 9 + 1);

       

      Numbers are generated on frame 5. "Test over" is frame 111.

      Capture.PNG

       

      Capture2.PNG

      On the "test over" frame I tried to use MeSelRam = null; but didn't work.

        • 1. Re: Stop math.random generating numbers
          ClayUUID Adobe Community Professional

          Uh... if you don't want that code to execute more than once, then don't loop to that frame more than once?

           

          That being said, I cannot fathom why you'd think generating a random number would break your buttons. Unless the values of MeSelRam and MeSelectQ are explicitly involved in your button event handler logic, there's no way it's having any effect on them.

          • 2. Re: Stop math.random generating numbers
            kglad Adobe Community Professional & MVP

            the following answers your question.  but before employing one of the two snippets read message 1 because there may be some other issue more fundamental than your question indicates.

             

            if you have code in a frame that you only want to execute once AND you need to replay that frame repeatedly, use either:

             

            // 1.

            var alreadyExecuted:Boolean;

            if(!alreadyExeucted){

            alreadyExecuted=true

            oneTimeF();

            }

             

            function oneTimeF():void{

            // the code you want to execute once

            }

             

            // or 2.

             

            var alreadyExecuted:Boolean;

            if(!alreadyExeucted){

            alreadyExecuted=true

            // put the code you want to execute once here

            }

            • 3. Re: Stop math.random generating numbers
              jackh8500410 Level 1

              I've just managed to fix it. I put the math.random() inside of my if statement;

               

              if (MeAnsweredQu == 10) {

                gotoAndStop("MeTestOver")

              } else {

                //Selecting random number

                var MeSelRam: Number = Math.random();

                MeSelectQu = Math.round(MeSelRam * 9 + 1);

               

              So now it will only generate if not all 10 questions have been answered. Thanks for both your help

              Jack

               

              EDIT: By doing this, it has also allowed my buttons to work on the test over frame!

              • 4. Re: Stop math.random generating numbers
                kglad Adobe Community Professional & MVP

                you're welcome.