Actionscript 3 你能解释一下3个随机交换形状的位置吗
我复制了一些代码,想使用它,但我不明白。这段代码是关于如何在特定位置随机交换形状的位置。任何人都可以简单地解释这段代码是如何工作的Actionscript 3 你能解释一下3个随机交换形状的位置吗,actionscript-3,flash-cs6,Actionscript 3,Flash Cs6,我复制了一些代码,想使用它,但我不明白。这段代码是关于如何在特定位置随机交换形状的位置。任何人都可以简单地解释这段代码是如何工作的 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 Poi
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;
}
有两个列表:MovieClips和Points。提供的脚本将其中一个列表随机化,因此每个MovieClip从给定的列表中获得一个随机点
随机排序的想法可能会有点混乱。Array.sort方法旨在根据给定的条件组织数组的元素,但如果您改为基于随机的条件,则元素将以没有预定义顺序的方式混合
另一种可能更容易理解的方法是将一个固定的MovieClip与一个随机点进行匹配,然后从相应的列表中删除匹配的对,然后继续,直到有项目剩下
var P:Array = [new Point(12, 42), new Point(43, 56), new Point(43,87)];
var M:Array = [mc1, mc2, mc3];
// Do while there are items to process.
while (M.length)
{
// Get the last MovieClip and remove it from the list.
var aMC:MovieClip = M.pop();
// Produce a random Point.
var anIndex:int = Math.random() * P.length;
var aPo:Point = P[anIndex];
// Remove the selected Point from its list.
P.splice(anIndex, 1);
// Move the selected MovieClip to the selected Point coordinates.
aMC.x = aPo.x;
aMC.y = aPo.y;
}
P.splice的功能是什么;还有M.pop@如果它们是阵列的内置功能,您可以在此处找到: