如何在移动物体撞击框架时将其停止?

时间:2012-08-05 12:08:33

标签: objective-c ios cocoa-touch touchesmoved

我正在做一个简单的游戏,用户可以将其中一个瓶子扔在顶部的状态栏上并打破它。我正在使用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任何建议!!

1 个答案:

答案 0 :(得分:0)

以下是我在游戏中进行碰撞检测的方法。

NSMutableArray *collidees = [NSMutableArray arrayWithArray:[[GRSceneController sharedSceneController] sceneObjects]]; 

[collidees removeObject:self];

NSMutableArray *collisions = [[NSMutableArray alloc] init];

position.y += speed.y;

for (GRBlock *obj in collidees) {
    if ([self.collider doesCollideWithCollider:obj.collider])
        [collisions addObject:obj];
}

if ([collisions count]) {
    [(GRSceneObject<GRColliderDelegate> *)self didCollideWith:collisions collisionType:kVerticalCollision];
}

[collisions removeAllObjects];

然后我就为position.x

重复同样的事情

doesCollideWithCollider:方法看起来像这样

-(BOOL)doesCollideWithCollider:(GRColliderObject *)aCollider {

    #if defined DEBUG_COLLIDER && defined LOG
    logRect(hitBox);
    logRect(aCollider.hitBox);
    #endif

    CGRect colliderBox = aCollider.hitBox;

    return CGRectIntersectsRect(hitBox, colliderBox);
}

hitBox只是一个围绕对象并充当对撞机的框,didCollideWith:collisionType:处理碰撞。

要取消这一点的主要原因是你应该有一个速度变量和position.y += speed.y来创建某种速度。更新position.y后检查垂直碰撞,更新position.x

后检查水平碰撞