【问题标题】:Collision Being Detected before Collision Takes Place在发生碰撞之前检测到碰撞
【发布时间】:2014-10-12 20:00:35
【问题描述】:

在下面的代码中,我尝试检测代码是否实际检测到碰撞。但在项目符号和线条碰撞之前,调试器会说“碰撞!”

//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!");
    }
}

我假设碰撞在它之前“发生”,因为我为子弹设置了位置。如何在不使用[UIView animateWithDuration] 的情况下“射击”子弹?或者,当碰撞实际发生时,我如何实际获取代码来检测碰撞?

【问题讨论】:

    标签: objective-c collision-detection


    【解决方案1】:

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

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

    如果你想做一个 2D 游戏,我推荐使用 Cocos2D 之类的东西。它为您想要做的事情提供高级支持。

    【讨论】:

    • 那么我把碰撞检测代码放在哪里呢?注意:Cocos2d 让我很困惑,所以我宁愿不用它
    猜你喜欢
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-05
    • 2019-04-30
    相关资源
    最近更新 更多