If with CGPointEqualToPoint do not work

淺唱寂寞╮ 提交于 2019-12-07 11:54:03

问题


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:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!