我正在努力学习为 Android 制作游戏。我正在尝试使图像沿斜边线移动。
我把数学和代码记下来了(我想,是从谷歌的某个地方得到的),但是当图像到达行尾时,会发生这种情况在目标点旁边的 2 个点之间“反弹”。就像它无法到达行尾,所以它只会到达它可以到达的最近的 2 个点,并在它们之间来回跳跃。
问题:我如何让它不从 2 点跳到斜边线末端的点?
这是我的代码:
// to find the distance between the 2 points
distX = endX - imaX;
distY = endY - imaY;
// find the hypotenuse
hyp = Math.sqrt(distX*distX + distY*distY);
// don't know what this is for
len = 160/hyp;
distX *= len;
distY *= len;
// and to move the image. The points have to be an int.
imaX += (int) (distX * 0.05);
imaY += (int) (distY * 0.05);
谢谢:) 编辑:添加了问题并修复了我在这里犯的错误。