as3数组不重叠随机排序

 

在写一个游戏时...要用到这样的排序....贴出来,,大家一起分享下............

package{
    import flash.display.Sprite;
    /**
     *
     * @author playerlife
     *
     */
    public class TestRandomArray extends Sprite{
        public function TestRandomArray() {
            var arr : Array = ["a", "b", "c", "d", "e", "f"];
            var arr2 : Array = ["aa", "bb", "cc", "dd", "ee", "ff"];
            var tmp : Array = randomSort2(arr, arr2)
                trace(tmp[0], tmp[1]);
        }
        
        //2个数组不重叠随机排序
        public function randomSort2(arg1 : Array = null, arg2 : Array = null) : Array{
            var len : int = arg1.length;
            var arr1 : Array = [];
            for (var i : int = 0; i < len; i++) {
                arr1[i] = i;
            }
            var arr2 : Array = [];
            arr2 = randomSort(arr1); //打乱
            var arr3 : Array = [];
            for (var m : int = 0, n : int = len - 1; m < len, n >= 0; m++, n--) //反转
            {
                arr3[m] = arr2[n]
            }
            var arr4 : Array = [];
            var arr5 : Array = [];
            for (var j : int = 0; j < len; j++) {
                arr4[j] = arg1[arr2[j]]
                    arr5[j] = arg2[arr3[j]]
            }
            return [arr4, arr5]
        }
        
        //数组随机排序
        public function randomSort(targetArray : Array) : Array{
            var arrayLength : Number = targetArray.length
                var tmpArr1 : Array = []
                for (var i : int = 0; i < arrayLength; i++) {
                    tmpArr1[i] = i
                }
                var tmpArr2 : Array = []
                for (var j : int = 0; j < arrayLength; j++) {
                    tmpArr2[j] = tmpArr1.splice(Math.floor(Math.random() * tmpArr1.length), 1)
                }
                var tmpArr3 : Array = []
                for (var k : int = 0; k < arrayLength; k++) {
                    tmpArr3[k] = targetArray[tmpArr2[k]]
                }
                return tmpArr3
        }
    }
}

 

 

posted @ 2012-10-17 11:28  playerlife  阅读(1000)  评论(0编辑  收藏  举报