我正在做一个简单的游戏,用户可以将其中一个瓶子顶在状态栏上并打破它。我正在使用touchesMoved来测试用户是否抓住一个瓶子(UIView),并且会响应用户的手指轻弹方向而抛出它。对于投掷部分来说,它是有效的,但是我希望它被破坏,如果投掷出框架,它会变成破碎的瓶子,这部分会让人头疼。下面是部分代码:如何在移动物体碰到框架时停止移动物体?
-(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];
}
现在,我所得到的是第一瓶飞出框架,很短的一段时间,似乎回到了框架的边缘,已经被打破,它真的看起来不自然。其实我正在考虑把它放在一个计时器来显示投掷,它可能会更好地管理。但我只是想探索是否有更简单的方法来做到这一点,没有计时器,thx任何建议!
什么是“GRSceneController”?无法找到它的开发者库,它来自OPENGL吗?我认为这超出了我所知。对不起,我在objective-c上仍然是新手,甚至还没有准备好开放GL。 – Walter 2012-08-05 15:32:14
哦,那只是我制作的一个场景控制器,你不会在任何地方找到它。这只是一个通用的指南,它不是复制和粘贴。 – 2012-08-05 17:44:50