2 Replies Latest reply on May 9, 2013 3:38 PM by gkaiseril

    Help with Arrays

    MrGawain

      Adobe Autocrat XI

      I am new to Java and I have been learing as much from the web as possible, but there is just too many non-reinvent material with few good examples not to ask for help.

       

      I am writing random generators for a game PDF.  In each case I am also trying to learn something new for JavaScript.  For this generator I wanted to try linear Arrays.

       

      The Script is attached to an OK button on Mouse Down.  Ideally it should suggest an eye color appropriate to the race chose by the user.

       

       

      ///// Eyes /////

      // Each race should have 20 eye colors repeating the most common colors

      // Ten Basic Colors = Amber, Black, Blue, Brown, Gray, Green, Hazel, Violet, Red, Yellow - not human  --- Variation can include Lt & Dk.  Races with limited color possibilities should us alternated description of the basic color.  Space is limited to 10 characters.

       

      var r4ce = this.getField("Race").value;

      var eyez = "";

      var resu1t = Math.ceil(Math.random()*20)-1;  // Should provide a result between 0 to 19

      var colorHuman = [ "Amber", "Black", "Lt Blue", "Blue", "Blue", "Dk  Blue", "Lt Brown", "Brown", "Brown", "Brown", "Dk Brown", "Dk Brown",  "Gray", "Green", "Lt Hazel", "Hazel", "Hazel", "Dk Hazel", "Violet",  "Red" ];

      var colorDark = [ "Amber", "Dk Amber", "Black", "Dk Blue", "Dk Blue", "Brown", "Brown", "Dk Brown", "Dk Brown", "Gray", "Green", "Dk Green",   "Dk Green", "Hazel", "Dk Hazel", "Dk Hazel", "Violet", "Dk Violet",  "Red", "Dk Red" ];

      var colorGreen = [ "Lt Olive", "Olive", "Dk Olive", "Lt Green", "Sea Green",  "Dk Green", "Lt Teal", "Teal", "Dk Teal", "Lt Green", "Green", "Dk  Green", "Lt Green", "Mint Green", "Dk Green", "Lt Lime", "Lime Green", "Dk Lime ", "Lt Green", "Green" ];

       

      // if (r4ce === 0) {app.alert pick a race.  // Alert works.  Skip

       

      if (r4ce === 1 || r4ce === 4) {

        eyez = colorHuman[resu1t].value; }     // The trouble is here = I have tried sever variations of the command but it doesn't return a string.

       

      else if (r4ce === 2) {

        eyez = colorDark[resu1t]; }

       

      else if (r4ce === 3) {                                 // A second issue is that some races have a small chance of having human colors.  (I'd like to add = if (resu1t === 1) {r4ce = 1; goto????;}  but I cant seem to find the correct goto command.

        eyez = colorGreen[resu1t]; }

       

      this.getField("Eyes").value = eyez;         // This finishes the code sending it to the Eye Text field.

       

       

      I know this is noob stuff but I just can seem to get the commands correct for Adobe.  Any help is welcomed. Thank you

        • 1. Re: Help with Arrays
          George_Johnson MVP & Adobe Community Professional

          This line:

           

          eyez = colorHuman[resu1t].value;

           

           

          Should be:

           

          eyez = colorHuman[resu1t];

          • 2. Re: Help with Arrays
            gkaiseril MVP & Adobe Community Professional

            For generating random numbers, I would use a function like:

             

            function RandomInt(max, min) {

            // generate random integer between min and max value;

            if(typeof min == "undefined") min = 0; // assume missing value is zero;

            // return random integer between min and max

            return Math.floor(Math.random() * (max - min +1)) + min;

            } // end RandomInt function;

             

            var resu1t = RandomInt(18);  // Should provide a result between 0 to 18 or 19 elements;

             

            or

             

            var result = RandomInt(18, 0);

             

             

            If you place the code for the function at the document level JavaScript, you can use the function in any field on the form.

             

            Arrays use a zero count for counting elements and not 1.

             

            You might want to try:

             

            var colorHuman = ["Amber", "Black", "Lt Blue", "Blue", "Blue", "Dk  Blue", "Lt Brown", "Brown", "Brown", "Brown", "Dk Brown", "Dk Brown",  "Gray", "Green", "Lt Hazel", "Hazel", "Hazel", "Dk Hazel", "Violet",  "Red"];

            console.println(typeof colorHuman + " " + colorHuman.length);
            for(i = 0; i < colorHuman.length; i++) console.println(i + ": " + colorHuman[i]);

             

            And observe the results in the console.