使用CGPointmake和NSSTimer的非常基本的动画?

时间:2014-07-23 20:17:15

标签: ios objective-c cocoa-touch animation nstimer

我在YouTube上观看了关于制作Flappy Bird克隆的教程。代码使用CGPointMake()一个整数变量和一个计时器来在屏幕上上下移动鸟,但我复制的代码不会导致鸟移动。

Viewcontroller.h

int BirdFlight;

@interface ViewController : UIViewController{

    IBOutlet UIImageView *Bird;
    IBOutlet UIButton *StartGame;

    NSTimer *BirdMovement;
}

- (IBAction)StartGame:(id)sender;
-(void)BirdMoving;

@end

Viewcontroller.m(所有更改)

@implementation ViewController

-(IBAction)StartGame:(id)sender{
    StartGame.hidden = YES;
    BirdMovement  = [NSTimer timerWithTimeInterval:0.05 target:self
                                          selector:@selector(BirdMoving) userInfo:nil repeats:YES];
}

- (void)BirdMoving {

    Bird.center = CGPointMake(Bird.center.x, Bird.center.y - BirdFlight);
    BirdFlight = BirdFlight - 5;

    if (BirdFlight < -15) {
        BirdFlight = -15;
    }

    if (BirdFlight > 0) {
        Bird.image = [UIImage imageNamed:@"BirdUp.png"];      
    }

    if (BirdFlight < 0) {
        Bird.image = [UIImage imageNamed:@"2.png"];
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {    
    BirdFlight = 30;
}

我不想使用任何其他方法制作动画我只是想知道如何让代码像在视频中一样工作。 (https://www.youtube.com/watch?v=RKG6fsM5JiA

1 个答案:

答案 0 :(得分:1)

[NSTimer timerWithTimeInterval:0.05 target:self
                          selector:@selector(BirdMoving) userInfo:nil repeats:YES];

应该是

[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(BirdMoving) userInfo:nil repeats:YES];