如果使用 CGPointEqualToPoint 不起作用 [英] If with CGPointEqualToPoint do not work

查看:63
本文介绍了如果使用 CGPointEqualToPoint 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚为什么这个函数不执行GameEnd函数,当球位置与块位置完全相同并且锚点相同时.

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];
        }
    }

推荐答案

CGPointEqualToPoint的实现是

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

...所以坐标必须绝对相等才能返回true.

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

CGFloat 类型并非总是如此,即使像素看起来是对齐的.您在动画或游戏模拟代码中的计算方式可能会导致微小错误.

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.

这篇关于如果使用 CGPointEqualToPoint 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆