繁体   English   中英

iPhone开发:碰撞检测不适用于动画

[英]iphone development: collision detection does not work for the animation

下面的代码仅从屏幕顶部创建一个小方块,然后移到底部。 我也有一个UIImageView(player),它由x轴上的加速度计控制。 目的是不触摸动画对象。 就像一个简单的赛车游戏。 尽管我没有错误,但我无法检测到碰撞。 我在代码中找不到错误。 可能是什么问题?

-(void)printOut:(NSTimer*)timer1;
{


    for (int i = 0; i < 100; i++) {

        p = arc4random_uniform(320)%4+1;

        CGRect startFrame = CGRectMake(p*50, -50, 50, 50);
        CGRect endFrame   = CGRectMake(p*50, CGRectGetHeight(self.view.bounds) + 50,
                                       50,
                                       50);

        animatedView = [[UIView alloc] initWithFrame:startFrame];
        animatedView.backgroundColor = [UIColor redColor];

        [self.view addSubview:animatedView];

        [UIView animateWithDuration:2.f
                              delay:i * 0.5f
                            options:UIViewAnimationCurveLinear
                         animations:^{animatedView.frame = endFrame;}
                         completion:^(BOOL finished) {[animatedView removeFromSuperview];}];


        CGRect movingFrame = [[animatedView.layer presentationLayer] frame];

        if(CGRectIntersectsRect(player.frame, movingFrame)){
            [timer invalidate];
            NSLog(@"asdad");

            UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"You Lost" message:[NSString stringWithFormat:@"You were hit!"] delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
            [alert show];
        }

    }

    [timer1 invalidate];

}

您无法检测到碰撞,因为系统对于动画唯一了解的帧值是开始帧和结束帧。 因此,除非您尝试检测碰撞的对象与第一个对象的开始或结束帧发生碰撞,否则您需要找到其他检测方法。 UIView动画是为美观而非游戏逻辑而设计的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM