Hello, im looking some information about how fix my problem please.
What i want to do: Create dynamic objets from one thread or anything else.
Problem: when the thread is running and trying to create the object then shows a EXC BAD ACCESS
i've been reading some topics about nsthreads and glgraphic and they said that i can't modify the GLGraphics with a thread. so i want to know other solutions to create a dynamic CCSpriteBatchNode objects
i hope someone know any solution for my problem. Thanks!
My snipet example:
i call this function on my main method
[NSThread detachNewThreadSelector:@selector(newMonster:) toTarget:self withObject:self];
this is how i create new monsters
-(void) newMonster:(Game*)myGame
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
while (TRUE)
{
sleep(3);
Monster *_monster = [Monster monsterWithGame:myGame];
[myGame addChild:_monster.batchNode z:6];
}
[pool release];
}
This is how i made the monster. on Monster.m
+(id) monsterWithGame:(Game*)game
{
return [[[self alloc] initWithGame:game] autorelease];
}
-(id) initWithGame:(Game*)game
{
cpShape *shape = [game.spacemanager addRectAt:cpvzero mass:STATIC_MASS width:30 height:30 rotation:0];
return [self initWithGame:game shape:shape];
}
-(id) initWithGame:(Game*)game shape:(cpShape*)shape;
{
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"walksprite.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"walksprite.png"];
[super initWithShape:shape spriteFrameName:@"walk1.png"];
_game = game;
self.autoFreeShape = YES;
self.spaceManager = game.spacemanager;
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 3; i++)
{
[walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache]
spriteFrameByName:[NSString stringWithFormat:@"walk%d.png", i]]];
}
CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.3f];
walkingAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:YES]];
[spriteSheet addChild:self];
self.batchNode = spriteSheet;
return self;
}
thanks...