游戏结束时存储变量和运行循环

时间:2014-05-14 16:47:58

标签: objective-c

我做了一个简单的游戏只是为了测试我的知识(类似于飞扬的鸟)。当游戏结束时,我想运行一个循环。循环将使鸟从当前Y位置下降到我选择的Y位置514(以模拟鸟落下并撞击地面)。这是我的GameOver方法:

-(void)GameOver{

if (ScoreNumber > HighScoreNumber) {
    [[NSUserDefaults standardUserDefaults] setInteger:ScoreNumber forKey:@"HighScoreSaved"];
}

[TunnelMovement invalidate];
[BirdMovement invalidate];

Exit.hidden = NO;
/*
TunnelTop.hidden = YES;
TunnelBottom.hidden =YES;
Bird.hidden = YES;
 */

}

所以我想保存鸟的当前Y位置。存储该变量并运行一个循环,使其从当前的y位置移动到y位置514.有人可以引导我完成该过程吗?感谢

2 个答案:

答案 0 :(得分:1)

如果你的鸟是UIView,例如。一个UIImageView,你可以使用UIView动画。

[UIView animateWithDuration:1.0
                              delay:0.0
                            options: UIViewAnimationOptionCurveEaseIn
                         animations:^{
                             bird.frame = CGRectMake(bird.frame.origin.x, 514, bird.frame.size.width, bird.frame.size.height);
                         }
                         completion:nil];

答案 1 :(得分:0)

你可以试试这个

-(void)GameOver
{

    if (ScoreNumber > HighScoreNumber) {
       [[NSUserDefaults standardUserDefaults] setInteger:ScoreNumber forKey:@"HighScoreSaved"];
       [[NSUserDefaults standardUserDefaults] synchronize];
    }

    [TunnelMovement invalidate];
    [BirdMovement invalidate];

    Exit.hidden = NO;
    /*
    TunnelTop.hidden = YES;
    TunnelBottom.hidden =YES;
    Bird.hidden = YES;
    */

  }

  - (void) startGame {
        NSInteger birdYLocation = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScoreSaved"];
       // assuming xOrigin, width and height are known values
       bird.frame = CGRectMake(xOrigin, birdYLocation, width, height); 

       [UIView animateWithDuration:1.0
                      delay:0.0
                    options: UIViewAnimationOptionCurveEaseIn
                 animations:^{
                     bird.frame = CGRectMake(bird.frame.origin.x, 514, bird.frame.size.width, bird.frame.size.height);
                 }
                 completion:nil];
     }