2011-05-09 4 views
1

그냥 레코드, 나는 AS3을 사용하고 있습니다.무작위로 배열을 제거

AS3에서 무작위로 스프라이트를 제거하고 싶습니다. 스프라이트를 만드는 방법을 알아 냈기 때문에 그리드로 채울 수있었습니다. 그들을 제거하는 방법을 밖으로!

여기에 내가 그들을 만드는 데 사용했던 코드입니다 :

function showpixels() : void 
{ 

    for (var i:int = 0; i < 40; i++) 
    { 
     for (var j:int = 0; j < 40; j++) 
     { 
      var s:Sprite = new Sprite(); 
      s.graphics.beginFill(0); 
      s.graphics.drawRect(i*10, j*10, 10, 10); 
      s.graphics.endFill(); 
      addChild(s); 
      pixels.push(s); 
     } 
    } 
} 

는 기본적으로 나는이 무작위로 아래에 볼 수 있는지까지 제거 할 필요가있다.

어떤 도움도 좋을 것입니다. 감사!

답변

1
function removeRandom():void 
{ 
    var rand:uint = Math.random()*pixels.length; 

    var i:Sprite = Sprite(pixels[rand]); 

    if(i.parent) i.parent.removeChild(i); 
    pixels.splice(rand, 1); 
} 

업데이트 :

var _timer:int = 100; 

addEventListener(Event.ENTER_FRAME, _handle); 
function _handle(e:Event):void 
{ 
    if(pixels.length > 0) _timer --; 
    if(_timer < 1) 
    { 
     _timer = 10 + Math.random()*50; 
     removeRandom(); 
    } 
} 

function removeRandom():void 
{ 
    var rand:uint = Math.random()*pixels.length; 

    var i:Sprite = Sprite(pixels[rand]); 

    if(i.parent) i.parent.removeChild(i); 
    pixels.splice(rand, 1); 
} 
+0

이봐 마티, 내가 노력하고 그게 뭔지의 일종 감사 도움이! 기본적으로 나는 모든 배경이 제거 될 때까지 모두 임의의 간격으로 제거되도록합니다. 이 고마워요. – Seb

+0

+1. 'removeChild (pixels.splice (Math.random() * pixels.length, 1) [0])' – back2dos

+0

로 축소 할 수 있습니다. 랩하지 않을 때 null 객체 참조 오류가 많이 발생하지만 removeChild ** ** if (부모) ** – Marty

1

마티의 아이디어 작품 : 임의의 간격으로 제거하려면 당신이 뭔가를 시도 할 수 있습니다. 또 다른 옵션은 먼저 배열을 섞어서 요소를 뽑아내는 것입니다.

셔플하려면 Arraypixels.sort(function (...args):int { return int(2*Math.random()-1) })을 사용하십시오.

그리고 당신은 간단하게 다음과 같이 제거 할 수 있습니다 :

function remove():void { 
    if (pixels.length) removeChild(pixels.pop()); 
    else clearInterval(this.id); 
} 

그리고 showpixels의 끝에 다음 행을 추가 :

this.id = setInterval(remove, 500);