Possible Duplicate:
CGRectIntersectsRect Problem
I am making a app with a maze, I put a ball inside the maze in the interface builder (I put an outlet for it) I have a touchesMoved:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint point;
point = [touch locationInView:self.view];
ball.center = point;
if (CGRectIntersectsRect(ball.frame, maze.frame)) {
//my stuff
}
}
I have two CGRectIntersectsRect if statements, I say, if the ball's frame touches the maze's frame, the开发者_如何学Cn // my stuff happens, but for some reason, whenever i try to move the ball, without touching the frame of the maze, // my stuff happens. I dont know why, maybe it is because the ball is IN the maze, probably not because i said if cgrectintersectsrect frame not bounds. so why is this happening?
CGRectIntersectsRect
is going to return true if any part of rect1 lies inside of rect2. So while your ball is completely inside your maze, your intersection test will always be true.
If you want to detect when the rect of your ball is touching the edge of the rect of your maze (rather than just anywhere in the maze), you need to test for !CGRectEqualToRect(CGRectIntersection(ball, maze), maze)
in addition to CGRectIntersectsRect(ball, maze)
.
精彩评论