处理中的平滑移动问题4



我正在编写一个程序,让小"鸟"四处移动并跟随光标。实例没有旋转或任何花哨的东西,我遇到了一个非常不寻常的问题。

如果实例处于相同的X级别或Y级别,则它会振荡。我以前遇到过边界和运动的问题,所以我试着采取类似的方法。

void move() {

if (xPos == mouseX) {
xPos += 0;
} else if (xPos < mouseX) {
xPos += speed;
} else {
xPos -= speed;
}

if (yPos == mouseY) {
yPos += 0;
} else if (yPos < mouseY) {
yPos += speed;
} else {
yPos -= speed;
}
xPos = (xPos > mouseX) ? xPos - speed : xPos + speed;
yPos = (yPos > mouseY) ? yPos - speed : yPos + speed;
}

只有当两个版本同时运行时,移动才会平滑。我已尝试重新排列">"和'<'标志无效。

我可以删除这个代码的任何部分以阻止它重复吗?

谢谢:(

当鸟靠近轴时,需要减少移动。试试这个:

if (abs(xPos - mouseX) < speed) {
xPos = mouseX
} else if (xPos < mouseX) {
xPos += speed;
} else {
xPos -= speed;
}

最新更新