动画影片剪辑在屏幕上随机跳跃

发布于 2024-10-03 11:32:30 字数 1066 浏览 0 评论 0原文

我有一个苍蝇的动画电影剪辑,它在随机位置生成,并通过从墙壁弹起而在屏幕上移动。但每次动画重新开始时,它似乎“跳”到一个随机位置。这是我在它产生时的代码:

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;

}

我如何修复它,以便苍蝇在每次动画开始时继续沿其适当的路径移动?

I have an animated MovieClip of a fly that spawns at a random location and moves across the screen by bouncing off the walls. But every time the animation starts over, it seems to "jump" to a random location. Here is the code I have for when it spawns:

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

  }

And this is the code for its movement:

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

}

How do I fix it so that the fly continues moving in its appropriate path every time the animation starts over?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽清梦 2024-10-10 11:32:30

如果我正确理解问题,那么在启动动画时,您必须检查它之前是否已经启动过。

一个简单的布尔变量就可以了:

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

这样它只会执行一次随机放置代码。

If I understand the problem correctly, when starting the animation you have to check if it has already been started before.

A simple boolean variable would do:

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

This way it'll only execute the random placing code once.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文