4 Replies Latest reply on Oct 17, 2012 12:49 AM by boxofspiders

    Using large arrays for a random word generator

    boxofspiders Level 1

      Hi there,

       

      I'm currently working on a random word generator quiz type thing, you push a button (click/touch) and a random word pops up, you then have to click/touch again to choose whether you think it's a real word or a made up word, each time you get one right you get a point. No problems so far, I have all this working using a few (eep) global variables and a couple of arrays that store the image names and the true/false values for whether they are real or not.

       

      Now that I am starting to populate the build with the final content I'm realising that the image array is going to be huge, some 250 plus entries and currently it's sitting on a button .click action. Is this the right way to be going about it or is there some better way for storing the array information? In a text or .js file file perhaps?

       

      Any advice much appreciated, thanks for reading!

        • 1. Re: Using large arrays for a random word generator
          resdesign Adobe Community Professional & MVP

          Do you mean to say that you have an image for each word? I usually swap images.

           

          For text, I use arrays all the time and it works well.

           

          You can also use a json file.

           

          See tutorial here: http://www.gotoandlearn.com/play.php?id=168

          1 person found this helpful
          • 2. Re: Using large arrays for a random word generator
            boxofspiders Level 1

            Hey, thanks for replying.

             

            Yes I have an image for each word and I'm using the array to load them randomly using this:

             

             

            var words=new Array()

             

            words[0]="url('images/word1.png')"

            words[1]="url('images/word2.png')"

            words[2]="url('images/word3.png')"

            words[3]="url('images/word4.png')"

            words[4]="url('images/word5.png')"

            words[5]="url('images/word6.png')"

             

            whichWord=Math.floor(Math.random()*(words.length))

            var randomWord = sym.createChildSymbol(words[whichWord], "wordWrapper")

             

             

            Only now it's got 200 odd entries it seems a bit wrong to have it all sat on a .click event ..

             

            I think an external file is the way to go, I'll have a look at that tut' tomorrow, thanks

            • 3. Re: Using large arrays for a random word generator
              TimJaramillo Level 4

              Hi Spiders,

               

              If you're pushing 200 objects in to an array, you probably want to use a "for" loop.

               

              Alternately, if you're loading an image randomly, and they're all named sequentially, you can try something like this:

               

              var wordCount = 200;

              var imageIDToLoad = Math.floor(Math.random()*wordCount);// choose random num

              var subString = 'images/word'+imageIDToLoad+'.png';// added this var to work around double quotes

              var imageURL = "url("+subString+")";// create URL based on random num ID and subString

               

              var randomWord = sym.createChildSymbol(imageURL, "wordWrapper");

               


              • 4. Re: Using large arrays for a random word generator
                boxofspiders Level 1

                Hi Tim,

                 

                That's neat, I did try something similar because yes, the images are sequentially named, I couldn't quite figure out how to combine the variable into the image url name though I thought there was a way, thank you. I think I'll probably go for this method as although the arrays are getting quite large the device itself is relatively simple so keeping it compact and contained works well for me.

                 

                Thanks both again for your replies, very much appreciated!