如果球到达球洞如何进行测试?

-1 objective-c ios

我在xcode中制作非常简单的应用程序.

我想说,如果球到达洞,比赛应该完成

所以我试着做.

if (ball . center == hole.center )
Run Code Online (Sandbox Code Playgroud)

和另一种方式,我失败了

我也尝试过这个

(ball.frame.origin.x == hole.frame.origin.x && ball.frame.origin.y == hole.frame.origin.y)
Run Code Online (Sandbox Code Playgroud)

和往常一样失败了

请帮忙.

我只是想要,如果球的fram触及洞游戏结束

Jac*_*ack 6

问题是你不应该检查一个位置是否完全相同,这不是它如何使用浮点坐标(我猜你正在使用)和游戏中物体移动的精度,不需要在同一个物体上有物体狭隘的立场.

您应该检查距离是否小于阈值:

float bx = ball.frame.origin.x;
float by = ball.frame.origin.y;

float hx = hole.frame.origin.x;
float hy = hole.frame.origin.y;

// you don't actually need abs since you are going to raise to the power of 2
// but for sake of soundness it makes sense
float dx = abs(bx-hx);
float dy = abs(by-hy);

if (sqrt(dx*dx + dy*dy) < THRESHOLD) {
  // the ball is enough near to center
}
Run Code Online (Sandbox Code Playgroud)