Colisión CGRect con múltiples CGRects

I am new in to iPhone programming, I am currently working on ballsplit game. In this game, when my bullet hits any ball ball, I want it to create two new balls. My bullet and ball are both uiimageview.

Aquí está mi código:

if ((CGRectIntersectsRect(bullet.frame,Ball.frame)) && !(ball)){
    ball=TRUE;
    x=Ball.frame.origin.x;
    y=Ball.frame.origin.y;
    [self performSelector:@selector(createball)];
}

Here is my create ball function..

-(void)createball{
    if (ball) {
        imageMove1 = [[UIImageView alloc] initWithFrame:CGRectMake(x,y,50 ,50)];  
        UIImage *imag = [UIImage imageNamed:@"ball1.png"];
        [imageMove1 setImage:imag];
        [self.view addSubview:imageMove1];
        [ballArray addObject:imageMove1];

        imageMove2 = [[UIImageView alloc] initWithFrame:CGRectMake(x,y,50 ,50)]; 
        UIImage *imag1 = [UIImage imageNamed:@"ball1.png"];
        [imageMove2 setImage:imag1];
        [self.view addSubview:imageMove2];
        [ballArray addObject:imageMove2];
        ball=FALSE;
        [self performSelector:@selector(moveball)];
    }
}

Now after creating these two uiimgeview, when bullet hits one of these two uiimageview I want it to create another two uiimageviews. But problem that I face how we can get the frames of these new uiimageview...

preguntado el 03 de mayo de 12 a las 18:05

Welcome to StackOverflow. As you are new here, please remember to read the Preguntas Frecuentes. Also remember that proper grammar and code formatting are required here, and that this is not a coders-for-hire website. -

1 Respuestas

After you move the bullet iterate trough array where you stored references to ball images:

for (UIImageView *ball in ballArray){
     //check for collision
    if (CGRectIntersectsRect(bullet.frame,ball.frame)){
        //hit
    }
}

Check collision between balls:

for (UIImageView *ballOne in ballArray){
    for (UIImageView *ballTwo in ballArray){
        //check for collision
        if (CGRectIntersectsRect(ballOne.frame,ballTwo.frame) && ballOne != ballTwo){
            //hit
        }
    }
}

contestado el 03 de mayo de 12 a las 19:05

thanx Alex for Quick response.but if my NSMutablearray have 4 balls at the same time then how to get the frame of balls which one hit by bullet.. - jamil

I've added example how to check for collisions between balls. - Alex

thanx for helping me.i try this..but its not work for me..bcz when my bullet hit the imagemove1.then how its possible to call createball function again.and also create new uiimageview with the same name.same imageview2 is still on the screen.. - jamil

Just call createBall where I've put //hit comment. And set the x,y coordinates to appropriate values. - Alex

Thanx dear @Alex but its not solve my problem.i already try it. - jamil

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.