HELP!我试图创建一个记忆游戏,我也不太清楚如何在随机设置X和Y位置形状的立场。他们不能重叠,这样他们必须在每个用户启动程序时间12不同的位置是随机的。
HELP! I'm trying to create a memory game and I'm not too sure how to randomize the shapes' positions in set x and y locations. They can't overlap so they must be randomized in 12 different locations every time the user starts the program.
试试:
function randomSort(a:*, b:*):Number
{
if (Math.random() < 0.5) return -1;
else return 1;
}
// Push 12 positions as new Point() in an array.
var positions:Array = [ new Point(12, 42), new Point(43, 56), new Point(43,87) ]; // ...add 12 positions
var mcs:Array = [mc1, mc2, mc3]; // ...add 12 mcs
positions.sort(randomSort);
// link randomized position to MovieClips:
for (var i:int = 0, l:int = positions.length; i < l, i++ ) {
var mc:MovieClip = mcs[i];
var point:Point = positions[i];
mc.x = point.x;
mc.y = point.y;
}