iphone-non-global uiimageview detection

2020-05-06 21:47发布

问题:

Background:I am using XCode 3.1.4, keep this in mind. Please do not comment about it however.

I have to butttons, fire and start. When fire is pressed, a UIImageView named one is created using the IBAction function. When start is pressed, a UIImageview named two is created. Then, when *one is created in the IBACtion function, I call another function using an NSTimer that passes *one as a userinfo paramater. Then I have a move function which moves *one by 20 pixels down the y axis. This works perfectly and I can create hundreds of UIImageViews that move simultaneously. I do the same thing with start, but with a different UIImage and *two as the userinfo paramater. That also works perfectly. However, I want to make a collision detector for the UIImageViews created from clicking these buttons. Since the pointers assigned to these buttons are not global, I dont know how to do this. I am posting my code below:

-(IBAction)startWaves:(id)sender {
        start.hidden=YES;
    fire.hidden=NO;
    [NSTimer scheduledTimerWithTimeInterval:1 target:self   selector:@selector(createMeteors) userInfo:nil repeats:YES];


}


-(void)createMeteors{

    UIImageView *one = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Meteor.png"]];
    CGRect rectOne = CGRectMake(arc4random() % (310), arc4random() % (1), 35, 35);
    [one setFrame:rectOne];
    [self.view addSubview:one];
    [NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(moveMeteorOne:) userInfo:one repeats:YES];





}

- (void)moveMeteorOne:(NSTimer *)timerOne {
    UIImageView *one = timerOne.userInfo;
    one.center=CGPointMake(one.center.x, one.center.y + 15); 
    if (CGRectIntersectsRect(one.frame, image.frame)) {
        background.image = [UIImage imageNamed:@"gameOver.png"]; 
        fire.hidden = YES; 
        image.hidden=YES; 

    }
}


-(void)createBullets{
    UIImageView *two = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"lazerBeam.png"]];
    CGRect rectTwo = CGRectMake((image.center.x), (image.center.y - 45), 7, 20);
    [two setFrame:rectTwo];
    [self.view addSubview:two];
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(moveBulletOne:) userInfo:two repeats:YES];

}

-(void)moveBulletOne:(NSTimer *)timerTwo {
    UIImageView *two = timerTwo.userInfo;
        two.center=CGPointMake(two.center.x, two.center.y - 15); 

}

-(IBAction)fireBullets:(id)sender {

    [self createBullets]; 

}