Search code examples
iosobjective-ccocoa-touchanimationnstimer

very basic animation using CGPointmake and NSSTimer?


I watched a tutorial on YouTube about making a Flappy Bird clone. The code used CGPointMake() with an integer variable and a timer to move the bird up and down the screen, but the code I've copied does not cause the bird to move.

Viewcontroller.h

int BirdFlight;

@interface ViewController : UIViewController{

    IBOutlet UIImageView *Bird;
    IBOutlet UIButton *StartGame;

    NSTimer *BirdMovement;
}

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

@end

Viewcontroller.m (all changes)

@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;
}

I don't want to use any other method to animate I'm just wondering how to make the code work like in the video. (https://www.youtube.com/watch?v=RKG6fsM5JiA)


Solution

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

    should be

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