AS2 Array random selection
How can I make an array in flash as2 and from there select 12 values assigning them to twelve different variables?
So far I got this:
quotes = new Array();
quotes[0] = "one";
quotes[1] = "two";
quotes[2] = "three";
quotes[3] = "four";
quotes[4] = "five";
quotes[5] = "six";
quotes[6] = "seven";
quotes[7] = "eight";
quotes[8] = "nine";
quotes[9] = "ten";
quotes[10] = "eleven";
quotes[11] = "twelve";
quotes[12] = "thirteen";
quotes[13] = "fourteen";
quotes[14] = "fifteen";
quotes[15] = "sixteen";
quotes[16] = "seventeen";
quotes[17] = "eighteen";
quotes[18] = "nineteen";
quotes[19] = "twenty";
Im keeping this structure because it will be easier to maintain in the long run and has more readability.
What I dont know is how to take twelve random values out of it and assign them to variables.
Ok, so I have now added this piece:
trace(quotes)
for(var i:Number = 0; i<12; i++){
var x:Number = Math.floor((Math.random()*quotes.length));
trace("X :: " + x);
trace("ARRAY VALUE :: " + quotes[x]);
开发者_开发问答 quotes.splice(x,1);
}
Now I see in the trace 12 different values without repetition. But still I dont know how to make the results be the values of 12 different vars.
var myArray = quotes.slice(); // make a copy so that the original is not altered //
n = 12;
for (var i:Number = 0; i < n; i++) {
var randomSelection = Math.floor((Math.random() * myArray.length));
trace("Selected: " + myArray[randomSelection]);
myArray.splice(randomSelection, 1);
}
Shamelessly taken and adapted from a random forum.
Math.random returns a number in the range [0-1), meaning it will never actually return 1, thus since you're flooring the value you're going to have to make the upper limit be n+1, where n is the true upper limit.
Now, it'd be good to know more about what the variables you want to use look like and whether or not they belong to the same object. I'm going to go ahead and assume that the variables are not named sequentially (i.e., prop1, prop2, prop3 etc.) but that they'll be set at the same time.
Thus, a solution would be:
// Store the variable names
var properties = [
"firstProperty",
"secondProperty",
"propertyThree",
"prop4",
"prop5",
"prop6",
"seventhProp",
"prop8",
"prop9",
"propTen",
"propEleven",
"property12"
];
var selection = quotes.slice(); // make a copy so that the original is not altered //
for (var i:Number = 0; i < properties.length; i++)
{
var randomIndex = Math.floor(Math.random() * (selection.length + 1));
// target is the object that holds the properties
target[properties[i]] = selection.splice(randomIndex, 1);
}
Here's another way of doing it, that allows for setting properties on different objects:
var i = 0;
var randomQuotes = quotes.sort(function()
{
return Math.round(Math.random() * 2) - 1;
});
target.prop = randomQuotes[i++];
target.prop2 = randomQuotes[i++];
other.prop = randomQuotes[i++];
// Keep going for all the properties you need to set
This could be abstracted away into a RandomQuote class, enabling you to re-use the functionality.
精彩评论