簡體   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