Problem with adding sprite using CCSprite array in cocos2d
I have problem with following code.
mySpriteArray=[[NSMutableArray alloc] init];
star=[CCSprite spriteWithFile:@"22.png"];
for(int i=0;i<10; i++)
{
[mySpriteArray insertObject:star atIndex:i];
}
// NSLog(@"x=%i",[mySpriteArray count]);
for (int i=0; i<10; i++) // Opponents is NSMutableArray
{
CCSprite *tempSprite = (CCSprite *) [mySpriteArray objectAtIndex:i];
tempSprite.position=ccp(100,100);
[self addChild:tempSprite];
}
} where star is a object of CCSprite and mySpriteArray is a mutable array.The problem is that when i run the program it crash and say
* Assertion failure in -[GameScene addChild:z:tag:], /Users/salimsazzad/Desktop/balon hunter/libs/cocos2d/CCNode.m:305 2010-10-08 19:05:35.854 balon hunter[3967:207] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'child already added. It can't be added again'.
i can't understand whats wrong,i am addin开发者_如何学JAVAg 10 object,not 1 object in 10 times because CCSprite *tempSprite = (CCSprite *) [mySpriteArray objectAtIndex:i];creating a new object each time. so what is the problem???
You have created your star object once and added it 10 times to array:
star=[CCSprite spriteWithFile:@"22.png"];
for(int i=0;i<10; i++)
{
[mySpriteArray insertObject:star atIndex:i];
}
So your array contains the same object and that's the reason of assertion you get.
精彩评论