constrain a touchesmoved object from colliding with walls?
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 wi开发者_高级运维th the "wall" and eventually got stuck there either.
I feel there must be a simple way to do that. could anyone enlighten me please?
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..
精彩评论