views:

59

answers:

1

Hi, I'm trying to create a very simple game where you can drag a simple imageView. The thing is there is a wall in the frame (just a rectangle) on which the image should not go. so I did something like this:

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
 UITouch *touch = [[event allTouches] anyObject];
 if ([touch view] == myImage) {
  if (CGRectContainsRect (CGRectMake(0, 0, 800, 768), [myImage frame]))
  {
   myImage.center = [touch locationInView:self.view];
  }
 }
}

But the problem was that the image did went out of it's bounds and then got stuck there and the touchesmoved ended.

so I've added this:

    else if (CGRectIntersectsRect (CGRectMake(801, 0, 223, 768), [myImage frame])) {
   CGPoint touchedPoint = [touch locationInView:self.view];
   myImage.center = CGPointMake(730, touchedPoint.y); 

  }

But this made the image to start flickering when intersecting with the "wall" and eventually got stuck there either.

I feel there must be a simple way to do that. could anyone enlighten me please?

A: 

ok found my mistake..

I had to do this:

if (CGRectContainsPoint (CGRectMake(0, 0, 800, 768), [touch locationInView:self.view]))

instead of this:

if (CGRectContainsRect (CGRectMake(0, 0, 800, 768), [myImage frame]))

ye, quite silly..

Uri