[英]is it possible to shuffle that three arrays everytime the game starts?
i created three arrays and each of them has their own sets of questions in it. 我创建了三个数组,每个数组都有自己的一组问题。 is it possible to shuffle that three arrays everytime the game starts?
每次游戏开始时,是否有可能洗牌三个数组? so that the user will have to answer different sets of question everytime he clicks for a new game.
这样,用户每次点击新游戏时就不得不回答不同的问题。
See for example: 参见例如:
The second one has a nice cheeky little use of Array.sort()
: 第二个使用
Array.sort()
很好用:
var arr:Array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
function randomize (a:*, b:*):int
{
return (Math.random() > .5) ? 1 : -1;
}
trace(arr.sort(randomize));
To shuffle the 3 arrays you could create 3 other arrays that will serve to hold the shuffled values. 要改组3个数组,可以创建3个其他数组来保存改组后的值。
var arr1:Array = [1, 2, 3, 4, 5];
var sorted1:Array = new Array(arr1.length);
var arr2:Array = [1, 2, 3, 4, 5];
var sorted2:Array = new Array(arr2.length);
var arr3:Array = [1, 2, 3, 4, 5];
var sorted3:Array = new Array(arr3.length);
randomPos:Number = 0;
you could then create a function that takes the value from one array and place it into a new shuffled array. 然后,您可以创建一个函数,该函数从一个数组中获取值并将其放入新的随机数组中。
function shuffleArray(x, y){
for(var i:int = 0; i < y.length; i++){
randomPos = int(Math.random() * x.length);
y[i] = x.splice(randomPos, 1)[0];
}
}
Then you would call the suffleArray function on each set of arrays: 然后,您将在每组数组上调用suffleArray函数:
shuffleArray(arr1, sorted1);
shuffleArray(arr2, sorted2);
shuffleArray(arr3, sorted3);
I hope this helps. 我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.