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];
}
}
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.
来源:https://stackoverflow.com/questions/21430020/if-with-cgpointequaltopoint-do-not-work