2012-05-03 89 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