2

I'm creating a program where an image of a bird continuously falls from the top of the screen (like "raining" birds). In order to have an NSTimer for each individual bird, I made a UIImageView subclass (called "BirdUIImageView"). However, I'm not sure how to implement the code correctly--where to put what, etc.

Here is the code I have in ViewController.m:

#import "ViewController.h"

#import "BirdUIImageView.h"

@interface ViewController ()

@end

@implementation ViewController {

    BirdUIImageView *_myImage;

}

- (void)viewDidLoad
{


    //IMAGE CREATOR TIMER
    createImagesTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(createImages) userInfo:nil repeats:YES];

}


  //CREATES AN IMAGE
-(void) createImages {  
    srand(time(NULL));
    int random_x_coordinate = rand() % 286;
    CGRect myImageRect = CGRectMake(random_x_coordinate, 0.0f, 40.0f, 40.0f);
    BirdUIImageView *myImage = [[BirdUIImageView alloc] initWithFrame:myImageRect];
    [myImage setImage:[UIImage imageNamed:@"flake.png"]];
    myImage.opaque = YES;
    [self.view addSubview:myImage];
    _myImage = myImage;

}

And here is the code I have in BirdUIImageView.m. I'm totally at a loss as to what to do in this file, but I made an attempt:

#import "BirdUIImageView.h"



@implementation BirdUIImageView  


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)viewDidLoad
{
    //FALLING BIRDS TIMER
    moveObjectTimer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(moveObject) userInfo:nil repeats:YES];
}

//FALLING BIRDS MOVER
-(void) moveObject {

    _myImage.center = CGPointMake(_myImage.center.x, _myImage.center.y +1);

}
user1824518
  • 213
  • 1
  • 4
  • 12
  • There's a lot of standard methods to animate view position nicely, do not use NSTimer for that – Vladimir Nov 27 '12 at 17:20
  • If I use those animate methods, would I be able to recall at some point later on the position of each falling bird image? (This will eventually be a game where I have to "catch" the objects in a basket image, so I'll need to recall the positions). I was under the impression that I have to move them position-by-position in order to get that to work – user1824518 Nov 27 '12 at 17:22

1 Answers1

3

First, remove the viewDidLoad and moveObject methods from your BirdUIImageView class, and then try this code below on your ViewController.m class. You can play around with the timers settings, to have your desired effect:

On ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    createImagesTimer = [NSTimer scheduledTimerWithTimeInterval:2.5
                                                         target:self
                                                       selector:@selector(createImages)
                                                       userInfo:nil
                                                        repeats:YES];
}


  //CREATES AN IMAGE
-(void) createImages {
    srand(time(NULL));
    int random_x_coordinate = rand() % 286;
    CGRect myImageRect = CGRectMake(random_x_coordinate, 0.0f, 40.0f, 40.0f);
    BirdUIImageView *myImage = [[BirdUIImageView alloc] initWithFrame:myImageRect];
    [myImage setImage:[UIImage imageNamed:@"flake.png"]];
    myImage.opaque = YES;
    [self.view addSubview:myImage];
    _myImage = myImage;

    [self move];
}


-(void)move {

    //FALLING BIRDS TIMER
    moveObjectTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(moveObject) userInfo:nil repeats:YES];


}


//FALLING BIRDS MOVER
-(void) moveObject {

    _myImage.center = CGPointMake(_myImage.center.x, _myImage.center.y +1);

}
neowinston
  • 7,584
  • 10
  • 52
  • 83