CGRectContainsRect 不工作

发布于 2024-12-29 03:15:45 字数 933 浏览 0 评论 0原文

我用这个方法有问题。我有两个显然彼此包含的矩形。(我什至手动绘制了它们的坐标图以确保。)当我使用 CGRectContainsRect 比较这两个矩形时,它返回 false。对于我的一生,我已经尝试了一切,搜索了网络,但我找不到这个问题的答案。有人知道为什么吗?当我调试时,我已经包含了 CGRect 的值,以表明它们肯定在彼此之内。

-(bool)checkBoundingBox {
    bool returnItem = YES;

    //Checks for sprite interaction
    for (int i = 0; i < [arrGameItems count]; i++) {
        CGRect rect2 = [[self getChildByTag:1] boundingBox];
        CGRect rect1 = [[self getChildByTag:3] boundingBox];

        //        rect1 = CGRectStandardize(rect1);
        //        rect2 = CGRectStandardize(rect2);

        if (CGRectContainsRect(rect2, rect1)) {
            CCLOG(@"removed child b*&ch");
            [self removeChildByTag:[arrGameItems count] cleanup:YES];
            returnItem = NO;
        }
    }   

    CCLOG(@"g-dammit");    
    return returnItem;
}

矩形1 原点 x = 141 y = 76,高度 = 25,宽度 = 25

矩形2 原点 x = 127 y = 91,高度 = 25,宽度 = 25

I'm having problems with this method. I have two rectangles that are obviously contained within each other.(I've even graphed their coordinates manually to make sure.) When I use CGRectContainsRect to compare these two rectangles, it returns false. For the life of me, I have tried everything, scoured the net, and I can't find an answer to this problem. Anyone have any idea why? I've included the values for the CGRects when I debug to show that they are definitely within each other.

-(bool)checkBoundingBox {
    bool returnItem = YES;

    //Checks for sprite interaction
    for (int i = 0; i < [arrGameItems count]; i++) {
        CGRect rect2 = [[self getChildByTag:1] boundingBox];
        CGRect rect1 = [[self getChildByTag:3] boundingBox];

        //        rect1 = CGRectStandardize(rect1);
        //        rect2 = CGRectStandardize(rect2);

        if (CGRectContainsRect(rect2, rect1)) {
            CCLOG(@"removed child b*&ch");
            [self removeChildByTag:[arrGameItems count] cleanup:YES];
            returnItem = NO;
        }
    }   

    CCLOG(@"g-dammit");    
    return returnItem;
}

rect1
origin x = 141 y = 76, height = 25, width = 25

rect2
origin x = 127 y = 91, height = 25, width = 25

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剪不断理还乱 2025-01-05 03:15:45

CGRectContainsRect() 检查一个矩形是否完全包含另一个矩形,而不仅仅是它们是否相交。从您的坐标来看,矩形不包含彼此,而只是相交。您正在寻找CGRectIntersectsRect()

CGRectContainsRect() checks if one rectangle completely encompasses another, not just if they intersect. From your coordinates, the rectangles don't contain each other, but just intersect. You're looking for CGRectIntersectsRect().

少女七分熟 2025-01-05 03:15:45

在您的示例中, rect1 不包含 rect2 。

矩形 1 x 坐标范围从 141 到 166。
矩形 2 x 坐标跨度从 127 到 152。

因此,矩形 2 不包含在矩形 1 中(因为矩形 2 存在于 x 坐标 127-140 内,而矩形 1 不存在于这些坐标中)。

rect1 does not contain rect2 in your example.

Rect 1 x coordinates span from 141 to 166.
Rect 2 x coordinates span from 127 to 152.

Therefor, rect2 is not contained within rect1 (because rect2 exists within x coordinates 127-140, and rect1 does not exist in those coordinates).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文