2013-10-27 43 views
1

我正在制作一个游戏,屏幕顶部的昆虫落下。游戏的目的是杀死这些昆虫。我已经为这些昆虫的移动方式做了一个代码,但是问题似乎是它们似乎以不平滑的方式旋转。他们抽搐! 这是代码:如何让我的对象在我的游戏中更平滑地旋转?

else // Enemy is still alive and moving across the screen 
    { 
     //rotate the enemy between 10-5 degrees 
     tempEnemy.rotation += (Math.round(Math.random()*10-5)); 
     //Find the rotation and move the x position that direction 
     tempEnemy.x -= (Math.sin((Math.PI/180)*tempEnemy.rotation))*tempEnemy.speed; 
     tempEnemy.y += (Math.cos((Math.PI/180)*tempEnemy.rotation))*tempEnemy.speed; 
     if (tempEnemy.x < 0) 
     { 
      tempEnemy.x = 0; 
     } 
     if (tempEnemy.x > stage.stageWidth) 
     { 
      tempEnemy.x = stage.stageWidth; 
     } 
     if (tempEnemy.y > stage.stageHeight) 
     { 
      removeEnemy(i); 

      lives--; 
      roachLevel.lives_txt.text = String(lives); 
     } 
    } 
} 

} ,我遇到的是一些昆虫沿着屏幕的边缘去另一个问题。用户几乎可以杀死他们,因为他们一半的身体在屏幕上,而另一半则关闭。我可以让它们从边缘移开一点,就像偏移一样?谢谢!

+0

你每一帧这样做呢? – Pier

+0

我正在对数组中的每个对象执行此操作。没有动画,全部由动作脚本提供动力 – user2896120

+0

如果在每个帧处更改某些内容(x,y,旋转),动画也可以通过动作发生。您错误地认为只有在时间轴上使用关键帧等时才会发生动画。这里有一本很好的书,只是用AS3中的代码来动画的东西。 http://www.amazon.com/Foundation-Actionscript-3-0-Animation-Making/dp/1590597915 – Pier

回答

0

从你的代码,它看起来像他们在颤抖,因为你被大量改变旋转马上:

tempEnemy.rotation += (Math.round(Math.random()*10-5)); 

相反,你应该插值/动画到你想要的旋转,而不是只跳权到它。有几种方法可以做到这一点,但不知道你的动画是如何设置的。

为了防止昆虫从屏幕边缘移动到屏幕边缘,可以放置偏移量并限制x/y位置。

如去:

var offset:int = 100; // limits the max x to be 100 pixels from the right edge of the stage 

if (tempEnemy.x > (stage.stageWidth - offset)){ 
    tempEnemy.x = stage.stageWidth - offset; 
} 
+0

我唯一拥有的动画是昆虫的双腿移动。旋转和移动由动作提供动力。我不想要一个大的旋转,只是一些旋转,昆虫可以在它向下移动时改变方向。旋转和移动都应该是随机的 – user2896120

+0

我会猜测并假设您的上面的代码正在每帧运行。直接每帧应用一点点随机旋转就会导致其本质上的颤抖行为。如果您绘制出以下数值,您可以看到这一点:Math.round(Math.random()* 10-5);您会看到一系列随机跳跃,产生锯齿线,而不是您想要的平滑动作。 – mitim

+1

我建议使用不同的方法来产生平滑的随机行为。有点复杂,但也许看看植绒算法(即转向/游荡部分)。 – mitim