tags:

views:

62

answers:

1

I have two UIImageViews located about center of a horizontal screen and when the user clicks a button another UIImageView, located off screen, slides in from the right. I'm am just trying to detect when the view being brought onto the screen collides with the two static views. The problem is when I run my code and check the CGRect frames they are returned from where the views start, not end, regardless of where I place the frame calls in my method or even if I place them outside the method in a separate one. I'm a bit new to Obj-C and I understand that Core Animation runs on a separate thread and I am guessing that is why I am getting the starting values. (Correct me if I am wrong here).

So, I guess the question here is how do I detect collisions when one item is static and another is animated. Here's my code (feel free to clean it up):

-(IBAction) movegirl {

//disabling button
self.movegirlbtn.enabled = NO;

//getting graphics center points
CGPoint pos = bushidogirl.center;
CGPoint box1 = topbox.center;
CGPoint box2 = bottombox.center;

//firing up animation
[UIView beginAnimations: nil context: NULL]; 
//setting animation speed
[UIView setAnimationDuration:2.0    ];

//setting final position of bushidogirl, then resetting her center position
pos.x =  260;
bushidogirl.center = pos;

//running animations
[UIView commitAnimations];

//playing gong sound
[self.audioplayer play];

//enabling button
self.movegirlbtn.enabled = YES;


[self collisiondetection: bushidogirl : topbox];

}

-(void) collisiondetection: (UIImageView* ) item1 : (UIImageView* ) item2 {

CGRect item1frame = item1.frame;
CGRect item2frame = item2.frame;

NSLog(@"Item1 frame = %d and Item 2 frame = %d", item1frame, item2frame);

}

+2  A: 

How about:

if (CGRectIntersectsRect(item1frame, item2frame))
{
   // collision!!
}
No one in particular
Thanks for the answer. I should have been a little more clear. I can code how to check and see if a collision occurs, just when I do so I am getting a negative result when they should be colliding.
PruitIgoe
How are you determining the item frames? Are you using the frames relative to a common parent view?
No one in particular
I might not be clear on what you are asking. If you mean by something like this: CGRect thisframe = parentObject.imageView.frame then no, their hierarchy is UIView-->UIImageView and I am referencing them as UIImageView.frame...I'm assuming the UIView is the parent here.
PruitIgoe