我有一个问题,如果球恰到好处地击中球拍,它就会被锁在里面,不能释放。它有时会发生在玩家的球拍上,但既然你可以控制它,你所要做的就是移动它,它就会逃逸。计算机完美地跟踪着球,所以一旦它进入球内,它就永远无法逃脱,导致了什么happens in this video。现在只有当我增加帧率时才会发生这种情况,如果帧率真的很低,它就会出错,并“穿过”划桨,从而获得一分。至少在这种情况下,它不是一个明显的错误,游戏继续玩。
当然,我希望帧速率尽可能平滑,所以……,我想解决这个问题。这是我用于非常简单的碰撞检测的代码。使用UIImageViews。
if (CGRectIntersectsRect (ball.frame, playerPaddle.frame))
{
if (ball.center.y < playerPaddle.center.y) //hits the "top" of the computer paddle
{
AudioServicesPlaySystemSound (hitSound);
ballVelocity.y = -ballVelocity.y; // turn ball around if collide
}
}
if (CGRectIntersectsRect (ball.frame, computerPaddle.frame))
{
if (ball.center.y > computerPaddle.center.y) //hits the "bottom" of the computer paddle
{
AudioServicesPlaySystemSound (hitSound);
ballVelocity.y = -ballVelocity.y; // turn ball around if collide
}
}谢谢你的帮助。
发布于 2009-10-14 03:02:04
除了上面的答案之外,你的球真的不应该被允许真正进入球拍。具体地说,如果球在球拍内,则应在切换其y速度的同时将其位置重置为仅在球拍之外。
具体地说:
if (CGRectIntersectsRect (ball.frame, playerPaddle.frame))
{
AudioServicesPlaySystemSound (hitSound);
CGRect frame = ball.frame;
frame.origin.y = playerPaddle.frame.origin.y - frame.size.height;
ball.frame = frame;
ballVelocity.y = -ballVelocity.y; // turn ball around if collide
}
if (CGRectIntersectsRect (ball.frame, computerPaddle.frame))
{
AudioServicesPlaySystemSound (hitSound);
CGRect frame = ball.frame;
frame.origin.y = CGRectGetMaxY(computerPaddle.frame);
ball.frame = frame;
ballVelocity.y = -ballVelocity.y; // turn ball around if collide
}发布于 2009-10-13 22:04:05
在看不到其余代码的情况下,这只是一个猜测,但可能发生的情况是球的速度不断地来回翻转。你需要在击球后将球移出球拍,或者有一个“两次碰撞的最短时间”。
发布于 2009-10-13 23:44:59
需要一个额外的条件。
if (ballVelocity.y > 0 && CGRectIntersectsRect (ball.frame, playerPaddle.frame))
{
if (ball.center.y < playerPaddle.center.y) //player
{
AudioServicesPlaySystemSound (volleyFileID);
ballVelocity.y = -ballVelocity.y;
}
}
if (ballVelocity.y < 0 && CGRectIntersectsRect (ball.frame, computerPaddle.frame)) //computer
{
if (ball.center.y > computerPaddle.center.y)
{
AudioServicesPlaySystemSound (volley2FileID);
ballVelocity.y = -ballVelocity.y;
}
}https://stackoverflow.com/questions/1563152
复制相似问题