이것은 충분히 간단하지만 빈 배열로 끝납니다.NSMutableArray를 다른 NSMutableArray에 추가하는 버튼의 NSMutableArray를 열거하십시오.
나는 눌러 진 단추를 가지고 그 옆에있는 모든 단추를 찾고 (코드의 주석 참조) 원하는 모든 단추를 다른 NSMutableArray에 추가하려고합니다.
그런 다음 배열을 반복하고 각 버튼을 사용하여 무언가를하고 싶습니다.
아래에서 im은 배열에 발견 된 버튼을 저장하기 위해 [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
물론, 버튼을 찾는 것이 잘 작동하지만 그들은 새로운 배열에 추가되지 않습니다> 난 그냥 이유를 이해 해달라고. 이것은 그것이 작동 해야하는 것 같습니다.
'NSMutableArray를 caualtiesArray * = [있는 NSMutableArray의 ALLOC] INIT]'사용 전에. – Akhilrajtr