2010-11-19 3 views
1

무작위로 생성 된 플라이의 동영상 클립이 있으며 벽에서 튀어 나와 화면을 가로 질러 이동합니다. 그러나 애니메이션이 시작될 때마다 은 임의의 위치으로 "점프"하는 것처럼 보입니다. 여기가 급부상 때의 내가 가지고있는 코드 :동영상이 무작위로 점프합니다.

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); 

    } 

는 그리고 이것은 그 운동에 대한 코드입니다 : 나는 즉시 이동 계속 있도록 문제를 해결하려면 어떻게

//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; 

}

애니메이션이 시작될 때마다 적절한 경로에 있습니까?

답변

3

정확하게 문제를 이해하면 애니메이션을 시작할 때 이전에 이미 시작되었는지 확인해야합니다.

간단한 부울 변수는 할 것 :

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); 
    } 
} 

는 한 번만 임의 배치 코드를 실행할 수 있습니다이 방법을.

+0

와우, 넣는 데 2 ​​초 밖에 걸리지 않았습니다. 완전히 고쳐졌습니다! 고맙습니다!!! – Lani

관련 문제