我收到错误消息:NSArrayM 在被枚举时发生了变异,我几乎尝试了在 stackoverflow 上能找到的所有东西。请记住,我在创建游戏时正在学习 :)
我正在使用精灵套件并在 didmovetoview 中使用 dispatch_async 来加载游戏。
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
// loading stuff, but also this that is causing the error:
[self buildmap];
dispatch_async(dispatch_get_main_queue(), ^(void){
// done loading
});
});
这是导致崩溃的构建图。
-(void)buildMap {
int intX = 0;
int intY = 0;
sqlite3_stmt *statement;
if (sqlite3_open([_databasePath UTF8String], &BlockMinerDB) == SQLITE_OK) {
NSString *sql = [NSString stringWithFormat:@"SELECT blocktype, ladder, blockreachable, walkable, zpos, texture, id, bitmask FROM map ORDER BY id DESC"];
const char *qstmt = [sql UTF8String];
if (sqlite3_prepare_v2(BlockMinerDB, qstmt, -1, &statement, NULL) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
int blockType = sqlite3_column_int(statement, 0);
int ladder = sqlite3_column_int(statement, 1);
int blockReachable = sqlite3_column_int(statement, 2);
int walkable = sqlite3_column_int(statement, 3);
int zPos = sqlite3_column_int(statement, 4);
NSString *texture = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 5)];
int idBlock = sqlite3_column_int(statement, 6);
uint32_t newBitmask = (uint32_t)[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 7)];
Blocks *testblock = [[Blocks alloc] initWithBlock:blockType blockId:idBlock ladder:ladder scene:self bitmask:newBitmask blockReachable:blockReachable walkable:walkable zPos:zPos texture:texture x:intX y:intY];
NSLog(@"%@: %i", testblock.name, blockType);
if ((blockType == 2) || (blockType == 3) || (blockType == 4) || (blockType == 5)) {
[blockArrayWalk addObject:testblock];
} else {
[blockArray addObject:testblock];
}
[background addChild:testblock];
intX++;
if (intX == 25) {
intX = 0;
intY++;
}
}
}
}
sqlite3_close(BlockMinerDB);
}
[self buildmap] 使用 sqlite 从数据库中检索地图,我正在使用 while 循环进行循环,这导致了崩溃。它在崩溃之前循环了大约 20-25 次(共 600 次)。
在循环中,我正在创建一个新块(SKSpriteNode 子类),它将块放置在某个位置并带有一些信息(位置、名称、物理等,没有重的东西)。我使用 NSMutableArray 来存储块以便于访问,并认为这是一开始的问题,但是当我删除 [blockArray addObject:block]; 该应用程序仍然崩溃。
如果我删除 -(void)buildmap 中的每一段代码,应用程序就不会崩溃。
dispatch_async 和 while 循环是否有可能导致这种情况?
我现在无法访问代码,但如果有人需要查看更多代码,我可以在大约 8 小时后添加它;) 非常感谢一些帮助,现在尝试解决这个错误 3 周 :D