diary       blog       guest

AS1.0 버전 : 배열요소를 섞는 방법

http://www.action-scripter.com/blog/trackback/904
// 배열요소를 섞는 방법
// AS1.0 버전 : 원본을 변형해서 새롭게 배열요소를 만든다.
Array.prototype.mx = function(){
        tmp = new Array();
        var len = this.length;
        for(var i=0;i<len;i++){
                 // 바로 랜덤을 push하지 말고 인텍스를 먼저 얻어낸다.
                var idx = random(this.length);
                tmp.push(this[idx]);
                this.splice(idx,1);
        }
        for(var i=0;i<tmp.length;i++){
                this[i] = tmp[i];
        }
};

a = new Array(0,1,2,3,4,5);
a.mx();
trace(a);

보헤가 알려준 가장 최적화된 소스
Array.prototype.randomA = function() {
   this.sort(function () {
      return Boolean(random(2));
   });
   return true;
};

2004/10/18 11:53 2004/10/18 11:53
   1    
ABOUT  |  WORKS  |  @seonggyu
COPYRIGHT ⓒ 2000 - 2010. ACTION-SCRIPTER.COM. ALL RIGHTS RESEVED.