Collection lt;__NSArrayM: 0x76c11b0gt; was mutated while being enumerated(集合lt;__NSArrayM:0x76c11b0gt;在枚举时发生了突变)
问题描述
我对 obj-c 比较陌生,所以我一定遗漏了一些东西,但是当敌人与墙壁相撞时,我的程序崩溃了.我已经找到了从循环中移除敌人的位置,虽然在循环中,但是对于我的生活,我无法弄清楚如何修复它.我的代码如下:
I'm relatively new to obj-c, so I must be missing something, but my program crash when an enemy collides with a wall. I've located where I'm removing the enemy from the loop, while in the loop, but for the life of me, i can't figure out how to fix it. My code is as follows:
(错误是[allEnemies removeObject:enemyType1];")
(the error is "[allEnemies removeObject:enemyType1];")
//一直在运行-(无效)更新:(ccTime)dt{
//ALWAYS RUNNING -(void) update:(ccTime)dt {
for (CCSprite *enemyType1 in allEnemies) { //for every attacking unit in allEnemies
//Adjust the collison box for each enemey depending on the height of the enemy
float a;
float b;
float yOne = (wall.contentSize.height-enemyType1.position.y);
float yTwo = (wall.contentSize.height);
float xTwo = 30;
a = (xTwo*(yOne/yTwo)); // always < 1
b = xTwo-a; // always > 1
//Create the altered collison box
CGRect enemyType1Rect = CGRectMake (
enemyType1.position.x - (enemyType1.contentSize.width/2),
enemyType1.position.y - (enemyType1.contentSize.height/2),
enemyType1.contentSize.width+b,
enemyType1.contentSize.height
);
//If the enemey hits the wall, stop it, then add it to the attacking enemies array
if (CGRectIntersectsRect(enemyType1Rect, wall.boundingBox)) {
[enemyType1 stopAllActions];
[allEnemies removeObject:enemyType1];
[attackingEnemies addObject:enemyType1];
}
}
//Wall Collison END
推荐答案
好吧,正如错误所述,您在枚举数组时对其进行了变异.最简单的解决方法是 for (CCSprite *enemyType1 in [[allEnemies copy] autorelease])
这样你就可以枚举数组的副本(这不会复制元素,只是给你另一个枚举它们的容器),并且仍然可以修改可变数组.
Well, just as the error states, you mutated the array while it was being enumerated. The easiest fix is to do for (CCSprite *enemyType1 in [[allEnemies copy] autorelease])
This way you're enumerating a copy of the array (this does NOT copy the elements, just gives you another container to enumerate them in), and can still modify the mutable array.
枚举时不能修改容器.
这篇关于集合<__NSArrayM:0x76c11b0>在枚举时发生了突变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:集合<__NSArrayM:0x76c11b0>在枚举时发生了突变
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01