10 Replies Latest reply: Jul 19, 2012 9:58 PM by abobo37 RSS

    Randomize Buttons on stage

    jdmrent1

      I'm new to flash and coding, I'm attempting to setup a quiz for students who are in elementary school. I have tried quiz creators but they don't do what i want them to do. I've tried captivate but that also doesn't support buttons that have images in them.

       

      My project consists of the following.

                                                                  the red _____

       

      apple.jpg               banana.jpg          orange.jpg

       

      The images are buttons which I made with roll over effects, and sound. They also move to the next question when the correct answer is chosen.

      What I want is for the Buttons to be randomized when the slide loads, so they are not always in the same spot.

      I created a layer called actions for the actionscript so in that layer, I have tried to create an array but I cant seem to make it work properly, the buttons never move. After extensive Google searches on randomization I have the following code.

       

      function Main() {

      var button:Array = [];
      button
      .push("choice1");
      button
      .push("choice2");
      button
      .push("choice3");
      ShuffleArray(button);
      trace
      (button);

      }
      function ShuffleArray(button:Array)
      {

      for (var i:int = button.length-1; i >=0; i--)
      {

      var randomIndex:int = Math.floor(Math.random()*(i+1));
      var itemAtIndex:Object = button[randomIndex];
      button
      [randomIndex] = button[i];
      button
      [i] = itemAtIndex;

       

       

      Needless to say I have no idea why its not randomizing. I'm sure I'm missing something, or not coded correctly.

       

      If someone could please point me in the right direction, or assist with the code I would really appreciate that.

       

      Thanks in advance.

        • 1. Re: Randomize Buttons on stage
          kglad MVP

          use:

           

           

          function Main() {

          var button:Array = [];
          button
          .push("choice1");
          button
          .push("choice2");
          button
          .push("choice3");
          shuffle(button);
          for(var i:int=0;i<button.length;i++){

          button[i].x = 50+button[i].width*i;

          }



          }

          function shuffle(a:Array) {

              var p:int;

              var t:*;

              var ivar:int;

              for (ivar = a.length-1; ivar>=0; ivar--) {

                  p=Math.floor((ivar+1)*Math.random());

                  t = a[ivar];

                  a[ivar] = a[p];

                  a[p] = t;

              }

          }

          • 2. Re: Randomize Buttons on stage
            jdmrent1 Community Member

            I tried your formula, no luck. i changed the "a" in your formula, to button the name of the array and still no success. So  here is what i have now.

             

            function Main() {

            var button:Array = [];
            button.push("choice1");
            button.push("choice2");
            button.push("choice3");
            shuffle(button);
            for(var i:int=0;i<button.length;i++){

            button[i].x = 50+button[i].width*i;

            }

             


            }

            function shuffle(button:Array) {

                var p:int;

                var t:*;

                var ivar:int;

                for (ivar = button.length-1; ivar>=0; ivar--) {

                    p=Math.floor((ivar+1)*Math.random());

                    t = button[ivar];

                    button[ivar] = button[p];

                    button[p] = t;

                }

            }

             

             

            again this is the actions layer, also the buttons were exported for actionscripting. I think im still doing something wrong or maybe, i inserted the wrong labels.

            • 3. Re: Randomize Buttons on stage
              kglad MVP

              you should be seeing error messages.  if not, check your publish settings to make sure you're publishing for as3.

               

              then, if you have buttons with instance names "choice1" etc, use:

               

              function Main() {

              var button:Array = [];
              button
              .push("choice1");
              button
              .push("choice2");
              button
              .push("choice3");
              shuffle(button);
              for(var i:int=0;i<button.length;i++){

              getChildByName(button[i]).x = 50+getChildByName(button[i]).width*i;

              }



              }

              function shuffle(a:Array) {

                  var p:int;

                  var t:*;

                  var ivar:int;

                  for (ivar = a.length-1; ivar>=0; ivar--) {

                      p=Math.floor((ivar+1)*Math.random());

                      t = a[ivar];

                      a[ivar] = a[p];

                      a[p] = t;

                  }

              }

              • 4. Re: Randomize Buttons on stage
                jdmrent1 Community Member

                I have tried this also, there were no errors that showed up however there is no change. the buttons remain on the same place/position on the stage.is there anything perhaps i should be doing differently? 

                 

                I did check the publish settings its set to as3, the advanced settings had something about doc class but i have no idea what that is or how to use it.

                • 5. Re: Randomize Buttons on stage
                  kglad MVP

                  what are the instance names of your buttons?

                  • 6. Re: Randomize Buttons on stage
                    jdmrent1 Community Member

                    my buttons are named choice1-3. i tried to make it as easy as possible for me once i start making the quiz i will change the name but i also know i have to change the code.

                    • 7. Re: Randomize Buttons on stage
                      kglad MVP

                      copy and paste the trace output from:

                       

                      function Main() {

                      var button:Array = [];
                      button
                      .push("choice1");
                      button
                      .push("choice2");
                      button
                      .push("choice3");
                      shuffle(button);
                      for(var i:int=0;i<button.length;i++){

                      getChildByName(button[i]).x = 50+getChildByName(button[i]).width*i;

                      trace(i,button[i],getChildByName(button[i].name)

                      }



                      }

                      function shuffle(a:Array) {

                          var p:int;

                          var t:*;

                          var ivar:int;

                          for (ivar = a.length-1; ivar>=0; ivar--) {

                              p=Math.floor((ivar+1)*Math.random());

                              t = a[ivar];

                              a[ivar] = a[p];

                              a[p] = t;

                          }

                      }

                      • 8. Re: Randomize Buttons on stage
                        jdmrent1 Community Member

                        The code you have gave no out put and also generated no errors. Still the same result.

                        • 9. Re: Randomize Buttons on stage
                          kglad MVP

                          then you're not even calling Main

                          • 10. Re: Randomize Buttons on stage
                            abobo37

                            um i'm new 2 this too,but maybe you could try something like

                             

                            trace("this is working");

                             

                            to make sure if the problem's in the code itself or if it's something else