简体   繁体   English

每次游戏开始时,是否有可能洗牌三个数组?

[英]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.

相关问题 游戏根据服务器时间开始倒计时 - Game starts countdown by server time AS3宾果游戏-有可能吗? - AS3 bingo game - is it possible? 将游戏排序项目拖放到三个目标 - Drag and drop game sort items to three targets 每当我从动作脚本3中的游戏结束屏幕重置游戏时,角色都会变得更快 - Character getting faster everytime I reset my game from gameover screen in action script 3 可以在预加载器启动之前加载非文档类吗? - Is it possible to load a non-Document Class before preloader starts? 动作脚本3。在菜单中时计时器开始计数,但游戏未开始 - Action Script 3. Timer starts count when I'm in menu, but game not started Starling版本缺少游戏Fla文件-可能恢复或反编译吗? - Game fla file missing from Starling build - possible recovery or decompile? 在游戏仍然平稳运行时,手机游戏消耗的最大内存是多少? - What is the most possible memory consumed by mobile game while game is still running smoothly? 为麻将之类的益智游戏提供路径和可能的解决方案 - Make the paths and possible solution for puzzle game like mahjong 是否可以将项目(游戏)从Flash CS4导入FlashDevelop? - Is it possible to import a project (game) from Flash cs4 to FlashDevelop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM