作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这看起来很简单,但我只是得到一个空数组。
我想获取一个按下的按钮,找到它旁边的所有按钮(参见代码中的注释),并将所有找到的按钮添加到另一个 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
显然,找到按钮工作正常,但它们没有被添加到新数组> 我只是不明白为什么。这似乎应该有效。
最佳答案
似乎您没有初始化 caualtiesArray。
caualtiesArray = [NSMutableArray 数组];
关于ios - 枚举按钮的 NSMutableArray 添加一些到另一个 NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21130540/
我是一名优秀的程序员,十分优秀!