2012-05-03 51 views
0

我是新来的iPhone编程,我目前正在研究ballsplit游戏。在这场比赛中,当我的子弹击中任何球时,我希望它创造出两个新球。我的子弹和球都是uiimageview。CGRect与多个CGRect碰撞

这里是我的代码:

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

这里是我创造球功能..

-(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)]; 
    } 
} 

现在创建这两个uiimgeview,当子弹击中了这两个的UIImageView之一后,我希望它再创建两个uiimageviews。但是,我面对怎样才能得到这些新的UIImageView的框架问题...

+2

欢迎来到StackOverflow。由于您是新手,请记得阅读[FAQ](http://stackoverflow.com/faq)。另外请记住,这里需要正确的语法和代码格式,而且这不是一个编码器雇用网站。 –

回答

2

后您将您存储引用球图像子弹迭代槽式阵列:球之间

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

检查碰撞:

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

thanx亚历克斯为快速response.but如果我的NSMutablearray同时有4个球,那么如何获得一个子弹击中球的框架。 – jamil

+0

我已经添加了示例如何检查球之间的碰撞。 – Alex

+0

thanx帮助我。我试试这个..但它不适合我..bcz当我的子弹击中imagemove1.then如何可能再次调用createball函数。并且还创建具有相同名称的新的uiimageview。imageame2是仍然在屏幕上.. – jamil