0

以下のコードは、画面の上から小さな正方形を作成し、下に移動するだけです。x軸の加速度計によって制御される UIImageView(player) もあります。エイムはアニメーション オブジェクトに触れていません。単純なレースゲームのように。エラーはありませんが、衝突を検出できません。コードに間違いが見つかりませんでした。何が問題になる可能性がありますか?

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

}
4

1 に答える 1

1

システムが認識できるアニメーションのフレーム値は開始フレームと終了フレームのみであるため、衝突を検出することはできません。したがって、衝突を検出しようとしているオブジェクトが最初のオブジェクトの開始フレームまたは終了フレームと衝突しない限り、それを検出する別の方法を見つける必要があります。UIViewアニメーションは、ゲームのロジックではなく美学のために設計されました。

于 2012-08-27T13:16:21.023 に答える