At 9/10/08 06:21 PM, Tivaelydoc wrote:
The only reason I didn't want to do it that way is because I have 10 different buttons that generate a different sentence amount. The first 2 only need 3 different variables, but the rest generate anywhere up to 12 different variables. Pretty much, I need different AS for each button.
Actually, no you don't. Check this out, you're gonna love it:
function flexibleGenerator(paramObject:Object):String {
//Never underestimate the power of for...in...
//To make this work, pass an object with references to as many arrays as you need.
var returnString:String = "";
for (var arrayRef in paramObject) {
returnString += paramObject[arrayRef][Math.floor(Math.random() * paramObject[arrayRef].length)] + " ";
}
return returnString;
}
If your arrays are called firstArray, secondArray, thirdArray, etc, you would call it as such:
var s:String = flexibleGenerator({ref1: firstArray, ref2: secondArray, ref3: thirdArray})
And it doesn't matter how many array references you cram in the object, the for...in loop will iterate through them all and choose a random index from each one. This reduces your AS writing to effectively 2 or 3 lines of code; call flexibleGenerator with the appropriate array references, set your text field's text to equal the string returned, and you're done. It doesn't solve the issue you're having with fonts, but at least it will ease the rest of the process.