2010-11-19 16 views
1

Rastgele bir yerde ortaya çıkan ve duvarların üzerinden zıplayarak ekran boyunca hareket eden bir sinekle canlandırılmış bir MovieClip'i var. Ama animasyon her başladığında, rastgele bir konuma "atlamak" gibi görünüyor. İşte onun çıktığı için ben kodudur:Animasyonlu MovieClip rastgele ekran etrafında atlar

private function beginClass(e:Event):void{ 
    _root = MovieClip(root); 

    do { 
    xRandom = Math.floor(Math.random() * 500); 
    yRandom = Math.floor(Math.random() * 350); 
    this.x = xRandom; 
    this.y = yRandom; 
    } while (Math.abs(xRandom - mouseX) > 20 && Math.abs(yRandom - mouseY) > 20); 

    } 

Ve bu onun hareketi için kod: Ben sinek hareket etmeye devam böylece düzeltmek nasıl

//Bouncing the fly off of the walls 
    if(this.x >= stage.stageWidth-this.width){ 
    //if the fly hits the right side 
    //of the screen, then bounce off 
    flyXSpeed *= -1; 
    } 
    if(this.x <= 0){ 
    //if the fly hits the left side 
    //of the screen, then bounce off 
    flyXSpeed *= -1; 
    } 
    if(this.y >= stage.stageHeight-this.height){ 
    //if the fly hits the bottom 
    //then bounce up 
    flyYSpeed *= -1; 
    } 
    if(this.y <= 0){ 
    //if the fly hits the top 
    //then bounce down 
    flyYSpeed *= -1; 

}

Animasyon her başladığında uygun yolda?

cevap

3

Sorunu doğru bir şekilde anlarsam, animasyonu başlatırken daha önce başlatılmış olup olmadığını kontrol etmeniz gerekir.

Basit boolean değişken yapacağını:

private var hasStarted:Boolean = false;  

private function beginClass(e:Event):void{ 
    _root = MovieClip(root); 

    if (!hasStarted) { 
     hasStarted = true; 

     do { 
      xRandom = Math.floor(Math.random() * 500); 
      yRandom = Math.floor(Math.random() * 350); 
      this.x = xRandom; 
      this.y = yRandom; 
     } while (Math.abs(xRandom - mouseX) > 20 && Math.abs(yRandom - mouseY) > 20); 
    } 
} 

yalnızca bir kez rastgele yerleştirme kodu çalıştırmak olacak Bu şekilde.

+0

Vay, bu sadece koymak için 2 saniye sürdü ve tamamen düzeltildi! Teşekkür ederim!!! – Lani

İlgili konular