2014-01-15 94 views
0

這似乎很簡單,但我最終只是一個空的數組。枚舉按鈕的NSMutableArray添加一些到另一個NSMutableArray

我想要一個按下按鈕,找到它旁邊的所有按鈕(請參閱代碼中的註釋),並將所有找到的按鈕添加到另一個NSMutableArray。

然後,我想循環訪問該數組,並對每個按鈕執行一些操作。

在下面的即時通訊使用[caualtiesArray addObject: btn];存儲在數組中找到的按鈕,但它似乎並沒有這樣做,我不知道爲什麼。

-(void)detonateBomb:(UIButton*)bombDetonated{ 
    //stop the spiketimers first 
    [spikeTimer invalidate]; 
    [removeSpikeTimer invalidate]; 
    //move bomb button (non enabled and hidden) over the pressed button 
    //really we are just going to use its rect to find buttons next 
    //to the bomb since its rect is lage enough to overlap the others 
    bombRadius.center=bombDetonated.center; 
    //create an array to hold any matches 
    //we cant use the original arrary 
    //because the array would be mutated while 
    //its being enumerated 
    NSMutableArray *caualtiesArray; 
     //I did try moving this to declare caualtiesArray 
     //in the .h....which didnt fix the issue 
    //loop through all of the unpopped buttons 
    //if any intersect with "bombRadius" they are next to the bomb 
    //add them to the array 
    for(UIButton *btn in mutableBubbleArray){ 
     if (CGRectIntersectsRect(btn.frame, bombRadius.frame)) { 
      [caualtiesArray addObject: btn]; 
      //^^This seems to be the problem 
      NSLog(@"------- added button: %ld", (long)[btn tag]); 
      NSLog(@"------- foundButtons: %ld", (long)[caualtiesArray count]); 
      NSLog(@"*******"); 
     } 
    } 
    //now that we have them all 
    //pop them and add an extra 50 pts each 
    //lets see how many we have first though 
    NSLog(@"------- foundButtons: %ld", (long)[caualtiesArray count]); 
    for(UIButton *foundBtn in caualtiesArray){ 
      score+=50; 
      [self popBubble:foundBtn]; 
     NSLog(@"------- popping button: %ld", (long)[foundBtn tag]); 
     NSLog(@"*******--------------------"); 
    } 
    //add an extra 100 pts 
    score+=100; 
    //move the bomb back off-screen 
    bombRadius.center=CGPointMake(-100, -100); 
    //clean the array 
    [caualtiesArray removeAllObjects]; 
} 

這是我在控制檯中:

2014-01-15 00:25:39.331 TestApp[7324:60b] ------- (pop method)POPPED A BOMB 
2014-01-15 00:25:39.333 TestApp[7324:60b] ********************************* 
2014-01-15 00:25:39.335 TestApp[7324:60b] ------- added button: 28 
2014-01-15 00:25:39.337 TestApp[7324:60b] ------- foundButtons: 0 
2014-01-15 00:25:39.339 TestApp[7324:60b] ******* 
2014-01-15 00:25:39.341 TestApp[7324:60b] ------- added button: 29 
2014-01-15 00:25:39.342 TestApp[7324:60b] ------- foundButtons: 0 
2014-01-15 00:25:39.345 TestApp[7324:60b] ******* 
2014-01-15 00:25:39.347 TestApp[7324:60b] ------- added button: 39 
2014-01-15 00:25:39.350 TestApp[7324:60b] ------- foundButtons: 0 
2014-01-15 00:25:39.352 TestApp[7324:60b] ******* 
2014-01-15 00:25:39.354 TestApp[7324:60b] ------- added button: 40 
2014-01-15 00:25:39.355 TestApp[7324:60b] ------- foundButtons: 0 
2014-01-15 00:25:39.357 TestApp[7324:60b] ******* 
2014-01-15 00:25:39.358 TestApp[7324:60b] ------- added button: 41 
2014-01-15 00:25:39.360 TestApp[7324:60b] ------- foundButtons: 0 
2014-01-15 00:25:39.361 TestApp[7324:60b] ******* 
2014-01-15 00:25:39.363 TestApp[7324:60b] ------- added button: 52 
2014-01-15 00:25:39.364 TestApp[7324:60b] ------- foundButtons: 0 
2014-01-15 00:25:39.366 TestApp[7324:60b] ******* 
2014-01-15 00:25:39.367 TestApp[7324:60b] ------- added button: 53 
2014-01-15 00:25:39.368 TestApp[7324:60b] ------- foundButtons: 0 
2014-01-15 00:25:39.370 TestApp[7324:60b] ******* 
2014-01-15 00:25:39.371 TestApp[7324:60b] ------- foundButtons: 0 

顯然,尋找按鈕工作正常,但他們沒有被添加到新陣列>我只是不明白爲什麼。這似乎應該工作。

+1

'的NSMutableArray * caualtiesArray = [[NSMutableArray裏的alloc] INIT];'使用前。 – Akhilrajtr

回答

3

似乎你並沒有初始化caualtiesArray。

caualtiesArray = [NSMutableArray array];

+0

啊!在我只使用它們之前:'mutableBubbleArray = [NSMutableArray arrayWithObjects:bubble1,bubble2,bubble3,nil];'沒有意識到我必須在創建空數組時進行初始化。現在完美! – DelightedD0D

相關問題