运行此代码并使子弹通过该行后,调试器中没有任何内容
//This is for the button that shoots the bullet
- (IBAction)shooterButton:(id)sender {
[self mover:(id)sender];
[self viewDidCollidee];
}
-(BOOL)viewsDoCollide {
CGRect bulletFrame = _bullet.frame;
CGRect lineFrame = _line.frame;
if(CGRectIntersectsRect(bulletFrame, lineFrame))
{
return YES;
}
return NO;
}
-(void)viewDidCollidee {
bool collisionDetector = [self viewsDoCollide];
if (collisionDetector == YES) {
NSLog(@"Collided!");
}
}
- (void)mover:(id)sender {
CGRect bulletFrame = _bullet.frame;
CGRect lineFrame = _line.frame;
[UIView animateWithDuration:0.2 animations:^{
CGRect newFrame = CGRectMake(bulletFrame.origin.x + lineFrame.origin.x + 15, bulletFrame.origin.y, bulletFrame.size.width, bulletFrame.size.height);
_bullet.frame= newFrame;
}];
}
我哪里错了?我已经检查过很多关于碰撞的问题,我看不到它。有没有其他方法来检测碰撞?如果没有,我哪里错了?
编辑:我接受了KirkSpaziani的建议,但改变了:
CGRect newFrame = CGRectMake(bulletFrame.origin.x + lineFrame.origin.x + 15, bulletFrame.origin.y, bulletFrame.size.width, bulletFrame.size.height);
要:
CGRect newFrame = CGRectMake(bulletFrame.origin.x + lineFrame.origin.x - 14, bulletFrame.origin.y, bulletFrame.size.width, bulletFrame.size.height);
注意,它是-14而不是+15。但是这样,即使图像还没有碰撞,也会检测到碰撞。如何在发生碰撞时检测到碰撞。
答案 0 :(得分:0)
看起来你只是在检查它们是否在你发起事件时发生碰撞
- (IBAction)shooterButton:(id)sender {
[self mover:(id)sender];
[self viewDidCollidee];
}
您可能希望每帧都调用viewDidCollidee(sic)。
[UIView animateWithDuration:0.2 animations:^{
CGRect newFrame = CGRectMake(bulletFrame.origin.x + lineFrame.origin.x + 15, bulletFrame.origin.y, bulletFrame.size.width, bulletFrame.size.height);
_bullet.frame= newFrame;
[self viewDidCollidee];
}];
这可能仍然不是你真正想要的,因为你可以通过你的对象而不检查对象的速度是否超过帧之间的另一个对象。