Ios 使用CGRectIntersectsRect更新我的游戏分数

Ios 使用CGRectIntersectsRect更新我的游戏分数,ios,objective-c,xcode,uiimage,Ios,Objective C,Xcode,Uiimage,我正在尝试使用CGRectIntersectsRect更新我的分数,并且每次两个图像相互碰撞时,我都希望增加我的分数。但是,我的分数并没有表现出周期性,有时它会增加一个单位,但当两幅图像以更高的速度碰撞时,它会随机增加。这是我的密码: -(void)Collision{ if (CGRectIntersectsRect(Ball.frame, Player.frame)) { PlayerScoreNumber = PlayerScoreNumber + 1; PlayerS

我正在尝试使用
CGRectIntersectsRect
更新我的分数,并且每次两个图像相互碰撞时,我都希望增加我的分数。但是,我的分数并没有表现出周期性,有时它会增加一个单位,但当两幅图像以更高的速度碰撞时,它会随机增加。这是我的密码:

-(void)Collision{

if (CGRectIntersectsRect(Ball.frame, Player.frame)) {

    PlayerScoreNumber = PlayerScoreNumber + 1;
    PlayerScore.text = [NSString stringWithFormat:@"%i", PlayerScoreNumber];

    Y = arc4random() %5;
    Y = 0-Y;

}

两个
ui图像
是“球”和“球拍”,每次球碰到球拍时,我想增加我的得分。请帮助……

我怀疑您正在计算两次(或更多)相同的碰撞,因此您必须保持碰撞状态,并记住您已经看到当前碰撞的时间:

在实现文件中创建一个新的实例变量,类似于:

@interface MyClass ()
{
    BOOL _ballCollidedWithPlayer;
}
并像这样管理国家:

-(void)Collision{

    BOOL collided = CGRectIntersectsRect(Ball.frame, Player.frame);
    if (collided) {
        if (_ballCollidedWithPlayer)
            return;    // Nothing to do; we already know about this collision

        PlayerScoreNumber = PlayerScoreNumber + 1;
        PlayerScore.text = [NSString stringWithFormat:@"%i", PlayerScoreNumber];

        Y = arc4random() %5;
        Y = 0-Y;
    }
    _ballCollidedWithPlayer = collided;
}