0

我试图让多个按钮以不同的速度同时从屏幕上掉下来。但是当我让我的if语句检查它是否通过值时,所有其他按钮都会随之消失。每次按钮通过 Y 值时,我也会将分数增加为 -1。任何帮助表示赞赏,谢谢。

- (void)b1Fall
{
b1.center = CGPointMake(b1.center.x, b1.center.y+6);

if (b1.center.y >= 506) {
    [self updateScore];
    b1.center = CGPointMake(44, 11);
  }
}

- (void)b2Fall
{
b2.center = CGPointMake(b2.center.x, b2.center.y+7);

if (b2.center.y >= 506) {
    [self updateScore];
    b2.center = CGPointMake(160, 11);
}
}

- (void)b3Fall
{
b3.center = CGPointMake(b3.center.x, b3.center.y+8);

if (b3.center.y >= 506) {
    [self updateScore];
    b3.center = CGPointMake(276, 11);
  }
}

- (void)updateScore
{
healthLabel.text = [NSString stringWithFormat:@"%d", [healthLabel.text intValue]-1];
}

- (void)viewDidLoad
{
[super viewDidLoad];

// REMOVE AFTER TESTING    
b1Timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(b1Fall) userInfo:nil repeats:true];
b2Timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(b2Fall) userInfo:nil repeats:true];
b2Timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(b3Fall) userInfo:nil repeats:true];
}
4

1 回答 1

0

一些东西:

1.) 将这些动画放在 viewDidAppear 而不是 viewDidLoad - 您希望动画在用户实际查看按钮时开始,而不是在视图加载到内存时开始,用户可能无法看到它 (viewDidLoad) .

2.) 不要将 NSTimer 用于动画,使用这个:

 [UIView animateWithDuration:0.5
                          delay:1.0
                        options: UIViewAnimationCurveEaseOut
                     animations:^{


                     } 
                     completion:^(BOOL finished){
                        // loop your animation here.

                     }];

3.) 如果你想制作游戏,我不建议使用这种方法。所有使用 UIKit 的动画都将在主线程上执行并阻塞用户流。你想要的是一个像 Cocos2D 这样的框架,其中动画在 GPU 中执行,并且很容易支持 ASYNC 游戏逻辑。UIKit 通常不是游戏开发的好选择。

于 2013-04-24T02:58:42.073 回答