幼稚的做法:
public function Main()
{
const array:Array = [1,2,3,4,5,6,7];
trace(array);
// 1,2,3,4,5,6,7
swapTwoRandomElements(array);
trace(array);
// 1,2,3,6,5,4,7
}
private function swapTwoRandomElements(input:Array):void
{
const indices:Array = [];
for (var i:int = 0; i < input.length; i++)
{
indices.push(i);
}
const indexFirst:int = indices[int(Math.random() * indices.length)];
indices.splice(indexFirst, 1);
const indexSecond:int = indices[int(Math.random() * indices.length)];
indices.splice(indexSecond, 1);
const tmp:* = input[indexFirst];
input[indexFirst] = input[indexSecond];
input[indexSecond] = tmp;
}
哪裏是X Y位置 –
@ActionScriptstudent你能請編輯您的問題呢?在「Y位置在哪裏」這個問題下,你不明白你的意思。根據一般問題,你想在數組中交換兩個隨機位置。在您的代碼示例中,您將創建一個數組數組,其中沒有X或Y座標。如果你想在你的box數組中使用交換函數,只需要調用:'swapTwoRandomElements(boxs);' – Nbooo
@ActionScriptstudent最簡單的方法是將初始位置定義爲一個點數組:'const positions:Array = [new點(20,20),...等]'。然後,只需使用交換功能來混洗這個數組。位置數組重新排列後,您需要將這些位置以適當的順序應用到剪輯中。對於動畫,你可以使用一些像TweenLite這樣的軟件包。 – Nbooo