I’m doing a simple game which user can throw one of the bottles against the status bar on the top and break it. I’m using touchesMoved to test if a user catch a bottle(UIView) and it will be thrown responding to the user’s finger flicking direction. For the throwing part, it works, but I want it to be broken and changed to a broken bottle png if it throws out of frame, this part gives it headache. Here is part of the codes:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *aTouch = [touches anyObject];
CGPoint location = [aTouch locationInView:self.view];
throwView = [self.view hitTest:location withEvent:nil];
//if the movement not on self.view, it's on the bottle.
if (bottleView != self.view) {
CGPoint loc = [aTouch locationInView:self.bottleView];
CGPoint prevloc = [aTouch previousLocationInView:self.bottleView];
//getting the finger moment
myFrame = self.bottleView.frame;
deltaX = loc.x - prevloc.x;
deltaY = loc.y - prevloc.y;
myLocation = bottleView.center;
//Here I try to multiply the finger movement to throw the bottles
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
myFrame.origin.x += deltaX*7.0;
myFrame.origin.y += deltaY*7.0;
[self.bottleView setFrame:myFrame];
[bottleView setNeedsDisplay];
[UIView commitAnimations];
//if it hits the top of the frame..
if (myFrame.origin.y <=20){
[self hitTheTop];
}
}}
//this part gives me headache, I repeat the code again otherwise the bottle is broken before hitting the top.
-(void) hitTheTop{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
myFrame.origin.x += deltaX*7.0;
myFrame.origin.y += deltaY*7.0;
[self.bottleView setFrame:myFrame];
[bottleView setNeedsDisplay];
[UIView commitAnimations];
//I calculate the slope and find where x should be when y is 0, 40 is half width of the bottle
myFrame.origin.x = myLocation.x + (myLocation.y / (-deltaY / deltaX)) - 40;
myFrame.origin.y = 0;
UIImageView *bottleBroken=
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"broken.png"]];
[bottleBroken setFrame:CGRectMake(-20, -30, 130, 180)];
[self.bottleView setFrame:myFrame];
[bottleView addSubview:bottleBroken];
}
For now, what i get is first the bottle flies out of frame, for a very short while, it appears back on the edge of the frame and is already broken, it really looks unnatural. Actually I’m thinking to put it a timer to display the throw and it may manage better. But I just want to explore if there is simpler way to do it without the timer, thx for any suggestion!!
Here’s how I do collision detection in my game.
Then I just repeat the same thing for
position.xthe
doesCollideWithCollider:method looks like thishitBoxis just a box that surrounds the object and acts as a collider, anddidCollideWith:collisionType:handles the collision.The main thing to take away from this is that you should have a variable for the speed and to
position.y += speed.yto create some sort of velocity. And check for a vertical collision after updatingposition.yand a horizontal collision after updatingposition.x