I started using reorderChild, and then I started to get random crashes. I noticed that it crashing in the reorderChild function.
-(void) reorderChild:(CCSprite*)child z:(int)z
{
// reorder child in the children array
[super reorderChild:child z:z];
// What's the new atlas index ?
NSUInteger newAtlasIndex = 0;
for( CCSprite *sprite in children_) {
if( [sprite isEqual:child] )
break;
newAtlasIndex++;
}
if( newAtlasIndex != child.atlasIndex ) {
[textureAtlas_ insertQuadFromIndex:child.atlasIndex atIndex:newAtlasIndex];
// update descendats (issue #708)
[child retain];
[descendants_ removeObjectAtIndex: child.atlasIndex];
[descendants_ insertObject:child atIndex:newAtlasIndex];
[child release];
// update atlas index
NSUInteger count = MAX( newAtlasIndex, child.atlasIndex);
NSUInteger index = MIN( newAtlasIndex, child.atlasIndex);
for( ; index < count+1 ; index++ ) {
CCSprite *sprite = (CCSprite *)[children_ objectAtIndex:index];
[sprite setAtlasIndex: index];
}
}
}
NSUInteger count = MAX( newAtlasIndex, child.atlasIndex);
this line returns different values every other time the code is run. When it returns 2 everything works, when it returns 3 it crashes. Whats weird is it returns the correct value lets say 1 out of 3 runs and does not crash.
causing this error:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (3) beyond bounds (3)
any ideas on this?