我正在制作一款游戏,游戏中昆虫从屏幕顶部落下。这个游戏的目的是杀死这些昆虫。我已经为这些昆虫做了一个关于它们如何移动的代码,但问题似乎是它们似乎以不平滑的模式旋转。他们抽筋了!代码如下:
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);
}
}
}
}我遇到的另一个问题是一些昆虫沿着屏幕的边缘移动。用户几乎不能杀死他们,因为他们一半的身体在屏幕上,另一半是关闭的。我可以让它们从边缘移动一点吗,比如偏移量?谢谢!
发布于 2013-10-27 12:39:09
从你的代码看,它们看起来像是在颤动,因为你马上就改变了大量的旋转:
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;
}
https://stackoverflow.com/questions/19614616
复制相似问题