1 Reply Latest reply on May 27, 2016 7:02 AM by kglad

    Actionscript Solution In Flash CS5, Creating A Random Name Generator.

    liamfrancis2013 Level 1

      Currently i have a pretty simple name generator that i have created in AS2.

       

      var nameArray:Array = new Array("name 1", "name 2", "name 3", "name 4", "name 5");

      function getName(){

      var ranNum = random(nameArray.length);

      var randomName = nameArray[ranNum];

      Text_txt.text = randomName;

      }

      Button_btn.onPress = function(){

      getName();

      }

       

      However, i want it to:

      Firstly cycle through the names in the array on screen until the button is pressed.

      Secondly for their to be an interactive way in which the user can enter the names to be added to the array, without having to go into the code. (for example the ability just to copy and paste a list of names into a text box, then click a button which adds this to an array, and deletes the last set of data already in the array)

       

      Thanks

       

       

        • 1. Re: Actionscript Solution In Flash CS5, Creating A Random Name Generator.
          kglad Adobe Community Professional & MVP

          you can randomize the names in your array (and the new one created by the user) by using the shuflle function that's below.

           

          you can add names using the push method but you'll probably need to do quite a bit of error checking or, even easier, allow your user to remove what they added.  have them use a designated delimiter between names (like a comma) so you can use the split method of strings to convert that into an array.

           

          then use an index to cycle through the names using a enterframe event and getTimer() to check when to update the name displayed.

           

           

          function shuffle(a:Array):Array {

              var len:Number = a.length-1;

              for (var ivar:Number = len; ivar>=0; ivar--) {

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

                  var t = a[ivar];

                  a[ivar] = a[p];

                  a[p] = t;

              }

              return a;

          }