2

I created a Thread like the below one:

[NSThread detachNewThreadSelector:@selector(connectionFinishedThread) toTarget:self withObject:nil];

inside this method, i created one sprite and given animation for this sprite. Animation not visible.

My code inside the Thread method:

CCSprite *aniSprite = [CCSprite spriteWithSpriteFrameName:@"r_anim01.png"];
aniSprite.position = ccp(50, 50);
[self addChild:aniSprite z:22];

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"r_anim1.plist"];

CCSpriteBatchNode *animSheet = [CCSpriteBatchNode batchNodeWithFile:@"r_anim1.png"];
[self addChild:animSheet];

NSMutableArray *animFrames = [NSMutableArray array];
for (int i=1; i<=6; i++) {
    [animFrames addObject:
     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
      [NSString stringWithFormat:@"r_anim1%d.png",i]]];
}

CCAnimation *anim = [CCAnimation animationWithSpriteFrames:animFrames delay:0.1f];
CCAction *spriteAction = [CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:anim]];

[sprite runAction:spriteAction];

Why it behaves like that?

Hari Babu
  • 891
  • 1
  • 9
  • 22
  • 2
    all graphics should be done on the main thread with cocos2d. reason for having your separate thread ? – YvesLeBorg Dec 12 '13 at 14:12
  • i need animation in Thread. because i have 10 animations at a time. Is it possible or not? – Hari Babu Dec 12 '13 at 15:01
  • yes it is possible (more even). It is all done on the main tread with cocos2d. What you have to pay attention to is the number of draws (if you are concerned with performance). If your sprites are all in a single texture, use a SINGLE batch node, and all animations will animate with a single draw call. Look at SpriteKit's doc to understand how the display link cycle works ... after all SK is heavily inspired from cocos2d :) – YvesLeBorg Dec 12 '13 at 15:10
  • one last word : try the framework as much as possible, if it is your first go at it. Dont use the simulator as an indication of performance of gamer experience. Target devices early in your project, the lowest entry device you intend to support, and use that as you litmus test of goodness. With one of my games, i get 8-12 fps on simulator and rock-steady 60fps on lowly devices (all on the main thread :) ). – YvesLeBorg Dec 12 '13 at 15:18

2 Answers2

0

Most changes to properties of CCNode classes must be done on the main thread. Cocos2D does not support multi-threading like Sprite Kit.

For example changing a sprite's texture from a background thread will certainly crash. But even subtle issues can occur because all properties are declared nonatomic.

The only way to use threads with cocos2d is to make sure whatever logic runs in the background thread does not directly change a node's properties. Ie doing some AI calculations in the background is fine, as long as AI actors aren't nodes but custom classes.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
0

You should not try to manipulate cocos2d objects (e.g. CCNode derived object) from another thread. The container holding the object (CCLayer, CCScene, etc.) may be manipulating it at the same time and none of the typical concurrency mechanisms (mutexes) are in effect.

If you need your sprite to take some kind of update action every time the frame updates, should schedule the container to update and update the sprite then. It is very common to move sprites around, update their orientations, etc., during the CCScene update.

FuzzyBunnySlippers
  • 3,387
  • 2
  • 18
  • 28