CGRectIntersection requires scalar type?

2019-09-19 01:53发布

问题:

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

    } 
}

回答1:

Use CGRectIntersectsRect, not CGRectIntersection.

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