碰撞发生前碰撞检测

时间:2014-10-12 20:00:35

标签: objective-c collision-detection

在下面的代码中,我尝试检测代码是否实际检测到了冲突。但是在子弹和线路发生碰撞之前,调试器会说"碰撞!"

//Collision Detector
-(BOOL)viewsDoCollide {
    if(CGRectIntersectsRect(_bullet.frame, _line.frame)) {
        return YES;
    } else {
    return NO;
    }
}

//So that the turrets shoot
- (IBAction)shooterButton:(id)sender {
    [self mover:(id)sender];
}

- (void)mover:(id)sender {
    CGRect bulletFrame = _bullet.frame;
    CGRect lineFrame = _line.frame;
    [UIView animateWithDuration:2 animations:^{
        CGRect newFrame = CGRectMake(bulletFrame.origin.x + lineFrame.origin.x - 14, bulletFrame.origin.y, bulletFrame.size.width, bulletFrame.size.height);
        _bullet.frame = newFrame;
    }];

bool collisionDetector = [self viewsDoCollide];

    if (collisionDetector == YES) {
        NSLog(@"Collided!");
    }
}

我认为碰撞发生在#34;在它之前,因为我有一个子弹的固定位置。我如何拍摄"子弹没有使用[UIView animateWithDuration]?或者,如何在实际发生时实际获取代码来检测碰撞?

1 个答案:

答案 0 :(得分:0)

您的碰撞检测代码不在正在运行的动画中。您已经要求操作系统从一个帧动画到另一个帧,这将在此函数返回后发生(在UI线程上)。

因此,您在动画开始前检测到碰撞。

如果您想进行2D游戏,我推荐使用像Cocos2D这样的东西。它为您的目标提供了高级支持。

相关问题