Я тестирую и прототипирую в SpriteKit и имею спрайт, который я переворачиваю двойным щелчком. У меня 52 спрайтов в _theArray
, в котором хранятся имена спрайтов, которые в настоящее время видны. Когда я выбираю спрайт Я хочу, чтобы быть на вершине в zPosition нить и достижения, которые я использую следующий код для обновления zPositions для всех спрайтов:Индекс NSMutableArray получает очень высокий и removeObjectAtIndex за пределами границ
- (void)setTheZposition:(NSString *)nodeNameToCheck {
NSLog(@"++setTheZposition:++");
if (![_node.name isEqual: @"background"]) { // Do not react if background
[self enumerateChildNodesWithName:nodeNameToCheck usingBlock:^(SKNode *node, BOOL *stop) {
NSUInteger index = [_theArray indexOfObjectPassingTest:
^(id obj, NSUInteger idx, BOOL *stop) {
return [obj hasPrefix:nodeNameToCheck];
}];
NSLog(@"index: %i & length _theArray: %i", index, [_theArray count]);
[_theArray removeObjectAtIndex:index];
[_theArray insertObject:nodeNameToCheck atIndex:0];
// Refresh the zPosition for all sprites
float positionZ = (int) [_theArray count];
for (NSString *theNode in _theArray) {
SKNode *refreshNode = [self childNodeWithName:theNode];
refreshNode.zPosition = positionZ;
positionZ--;
}
}];
}
}
код работал поиск до тех пор, как я только тащили спрайте, но когда я заменю спрайт и удалю оригинал, я удаляю index = 0
и добавление index = 0
в _theArray
, и это то, что, по-видимому, вызывает проблему.
Катастрофа произойдет в следующей строке:
[_theArray removeObjectAtIndex:index];
... с индексом бытия: 2147483647
... в то время как она должна быть 0
.
Краш-журнал:
2013-09-29 21:08:59.445 testButtonsetc[11597:a0b] ++setTheZposition:++
2013-09-29 21:08:59.446 testButtonsetc[11597:a0b] index: 51 & length _theArray: 52
2013-09-29 21:09:04.112 testButtonsetc[11597:a0b] ++setTheZposition:++
2013-09-29 21:09:04.112 testButtonsetc[11597:a0b] index: 0 & length _theArray: 52
2013-09-29 21:09:04.343 testButtonsetc[11597:a0b] >>>>>>>>T-A-P<<<<<<<<<
2013-09-29 21:09:07.277 testButtonsetc[11597:a0b] ++setTheZposition:++
2013-09-29 21:09:07.278 testButtonsetc[11597:a0b] index: 0 & length _theArray: 52
2013-09-29 21:09:07.477 testButtonsetc[11597:a0b] >>>>>>>>T-A-P<<<<<<<<<
2013-09-29 21:09:10.851 testButtonsetc[11597:a0b] ++setTheZposition:++
2013-09-29 21:09:10.851 testButtonsetc[11597:a0b] index: 2147483647 & length _theArray: 52
2013-09-29 21:09:10.852 testButtonsetc[11597:a0b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM removeObjectAtIndex:]: index 2147483647 beyond bounds [0 .. 51]'
Я бы очень признателен за любую помощь, которую я могу получить.
интересно, почему -1? – PeterK