If with CGPointEqualToPoint do not work

2019-05-10 19:50发布

Im trying to figure out why this function do not execute theGameEnd function, when ball position is exactly the same as block position and when anchorpoints are the same.

 if (CGPointEqualToPoint(ball.position,block.position)) {
        if (CGPointEqualToPoint(ball.anchorPoint,monster1.anchorPoint)) {
            [self theGameEnd];
        }
    }

1条回答
啃猪蹄的小仙女
2楼-- · 2019-05-10 20:15

The implementation of CGPointEqualToPoint is

CG_INLINE bool __CGPointEqualToPoint(CGPoint point1, CGPoint point2)
{
    return point1.x == point2.x && point1.y == point2.y;
}

... so coordinates have to be absolutely equal to return true.

This is not always the case with CGFloat types, even if pixels seem to be aligned. There might be tiny errors resulting from the way you calculate them in your animation or game simulation code.

You could try to round the values before comparing them or allow for a small deviation.

查看更多
登录 后发表回答