how can we add an object to a NSMutableArray at index 0, when the first index was previously removed? insertObject does not work :
Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘* -[__NSArrayM removeObjectAtIndex:]: index 2 beyond bounds [0 .. 1]’
When the screen moves to the right, i remove the first index of the array (which is not visible anymore) and add it to the right. But if i want to go back to the left, i have to add the previously removed image (now at index 2, but the images are the same, so it does not really matter if i just add another image). addObject adds an object at the end of the array, and insertObject throws an error…
Would you know any way to fix this?
linesArray = [[NSMutableArray alloc] initWithObjects:lines1, lines2, lines3, nil];
//left direction : add the object at index 0
if (offsetParam >= offset1){
CCSprite *temp = [[sprites objectAtIndex:1] retain];
[sprites removeObjectAtIndex:2];
[sprites insertObject:temp atIndex:0];//****CRASH HERE****
temp.position = ccp(-offset1-spriteWidth, temp.position.y);
[temp release];
offset1 += spriteWidth;
}
Thanks
If you read that exception a little closer
You will see that it’s actually a crash on the line right before the one you mention. This line is fine:
It the previous one that crashes:
It looks like you have an array with 2 items, (indices 0 and 1) so removing index 2 crashes because there is no index 2.