CGRectContainsRect 不工作
我用这个方法有问题。我有两个显然彼此包含的矩形。(我什至手动绘制了它们的坐标图以确保。)当我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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 forCGRectIntersectsRect()
.在您的示例中, 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).