CGRectIntersection是否需要标量类型?
问题描述:
我正在尝试检查 NSMutableArray
中的对象与另一个对象的碰撞(使用 CGRect
),但是它一直说该方法需要标量类型?!
I am trying to check collisions of objects from a NSMutableArray
against another object (using a CGRect
) but it keeps saying the method requires a scalar type?!
这里是抛出错误的方法:
Here is the method throwing the error:
-(void) checkSquareToCircleCollisions{
NSMutableArray *array = [squares getSquares];
for(int i = 0; i < [squares getCount]; i++){
Square *s = [array objectAtIndex: i];
CGRect rect1 = [player getRect];
CGRect rect2 = [s getRect];
//if(CGRectIntersection(rect1, rect2)){
//[player setAlive: NO];
// }
}
}
答
使用 CGRectIntersectsRect
,而不是 CGRectIntersection
。
CGRectIntersectsRect
返回布尔值:如果矩形相交,则返回YES。 CGRectIntersection
返回 CGRect
,它是两个矩形之间的重叠(如果有)。
CGRectIntersectsRect
returns a boolean: YES if the rectangles intersect. CGRectIntersection
returns the CGRect
that is the overlap (if any) between the two rectangles.
if (CGRectIntersectsRect(playerRect, squareRect)) {
player.alive = NO;
}