2010-11-19 67 views
1

我有一个动画MovieClip,它在随机位置产生并通过反弹从屏幕上移过屏幕。但每次动画重新开始,它似乎“跳”到一个随机位置。这里是我的代码有它产生的时候:动画MovieClip随机跳过屏幕

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 2010-11-19 21:27:01