-
1. Re: Using large arrays for a random word generator
resdesign Oct 16, 2012 8:02 AM (in response to boxofspiders)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
-
2. Re: Using large arrays for a random word generator
boxofspiders Oct 16, 2012 9:00 AM (in response to resdesign)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 Oct 16, 2012 11:45 AM (in response to boxofspiders)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 Oct 17, 2012 12:49 AM (in response to TimJaramillo)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!



