简体   繁体   English

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

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

The code below simply creates small squares from top of the screen and moves to the bottom. 下面的代码仅从屏幕顶部创建一个小方块,然后移到底部。 I also have an UIImageView(player) which is controlled by the accelerometer on the x axis. 我也有一个UIImageView(player),它由x轴上的加速度计控制。 The aim is not touching the animated object. 目的是不触摸动画对象。 like a simple race game. 就像一个简单的赛车游戏。 Although I have no error I cant detect the collision. 尽管我没有错误,但我无法检测到碰撞。 I could not find the mistake in my code. 我在代码中找不到错误。 What can be the problem? 可能是什么问题?

-(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];

}

You cannot detect the collision because the only frame values the system is ever aware of for your animation are the beginning frame, and the end frame. 您无法检测到碰撞,因为系统对于动画唯一了解的帧值是开始帧和结束帧。 So unless the object you are trying to detect collision of collides with the beginning or end frame of the first object, you need to find a different way to detect it. 因此,除非您尝试检测碰撞的对象与第一个对象的开始或结束帧发生碰撞,否则您需要找到其他检测方法。 UIView animations were designed for aesthetics not game logic. UIView动画是为美观而非游戏逻辑而设计的。

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

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