Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy am not getting collision detection with CGRectIntersectsRect in IOS?
    primarykey
    data
    text
    <p>I have 4 elements: greenball, yellowball, orangeball, and redball that fall from the top of the screen</p> <p>I also have an element, blueball, that follows my touch. </p> <p>All of these things are working great! :D</p> <p>However, I want to detect when the greenball intersects with the blueball</p> <p>my blue ball is placed in the view at viewDidLoad, the 4 balls are placed on the view based on an nstimer that calls onTimer.</p> <p>All 5 balls are created on the same layer:</p> <pre><code>[self.view insertSubview:greenImage belowSubview:bottomBar] </code></pre> <p>I can detect when the redball and greenball collide:</p> <pre><code>if (CGRectIntersectsRect(greenImage.frame, redImage.frame)) { UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"title" message:@"points" delegate:nil cancelButtonTitle:@"cool" otherButtonTitles:nil]; [message show]; [message release]; } </code></pre> <p>If I replace "redImage" for "blueball" I don't get the alert. I assume this may be because they aren't declared in the same method. </p> <p>Any ideas on how to detect this collision?</p> <p>thanks!</p> <p><strong>--- EDIT ---</strong></p> <p>Using the suggestion below I'm doing the following:</p> <pre><code> greenImage = [[UIImageView alloc] initWithImage:green]; greenImage.frame = CGRectMake(startX,0,43,43); [self.view insertSubview:greenImage belowSubview:bottomBar]; [UIView beginAnimations:nil context:greenImage]; [UIView setAnimationDuration:5 * speed]; greenImage.frame = CGRectMake(startX, 500.0, 43,43); CGFloat r1 = greenImage.frame.size.width * .5; CGFloat r2 = blueBall.frame.size.width * .5; if(CGPointDistance(greenImage.center, blueBall.center) &lt; (r1 + r2)){ UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"notice" message:@"hit!" delegate:nil cancelButtonTitle:@"cool" otherButtonTitles:nil]; [message show]; [message release]; } </code></pre> <p>below this method I have:</p> <pre><code>CGFloat CGPointDistance(CGPoint p1, CGPoint p2) { return sqrtf((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y)); } </code></pre> <p>get an error:</p> <blockquote> <p>conflicting types for 'CGPointDistance'</p> </blockquote> <p>and a warning:</p> <blockquote> <p>implicit declaration of function 'CGPointDistance'</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload