1

I am animating a whole body through SpriteSheets with CCSpriteBatchNode and CCSpriteFrameCache. Now user can add his own pic to that body which when i try to addChild to Spritesheet crashes with error "CCSprite is not using the same texture id"

Now i know the face CCSprite was not in that cache/texture(it was created through texturepacker) and the crash was normal but i wanted to know if there was a workaround to this as i have to add a face to that body through user interaction and animate that body. And by far using spritesheets is the best option for animation. anyone??

hemant
  • 1,771
  • 4
  • 31
  • 43

3 Answers3

2

In this case what you can do is You take picture of user , then you make texture from user's image . Then Add that texture to the CCTextureCache . Now you have texture of user image. Now you can use that texture in animation.

Make Texture from Sprite(You can make sprite from user image)

CCSprite *spr = nil;//your sprite
CCRenderTexture* renderTexture = [CCRenderTexture renderTextureWithWidth:spr.contentSize.width height:spr.contentSize.height];

spr.anchorPoint = ccp(0, 0);
spr.position = ccp(0, 0);
[renderTexture addChild:spr];  

[renderTexture begin];     
[spr draw]; // or [spr visit];
[renderTexture end];

CCTexture2D *result = renderTexture.sprite.texture;

Add that Texture in to Texture Cache.

[[CCTextureCache sharedTextureCache] addTexture]

Renaissance
  • 564
  • 5
  • 26
0

You can not Manually add CCSprite to SpriteSheets. Because When you create animated Sprite using texturepacker then i hope that you know it also created with .plist file and it loas image from it with its size.

When you add manually CCSprite then it is nor found from SpriteFramesWithFile. may be you got error.

another way for add animated CCSprite without use of texturepacker

CCSprite *dog = [CCSprite spriteWithFile:@"dog1.gif"];
        dog.position = ccp(winSize.width/2, winSize.height/2);
        [self addChild:dog z:2];

        NSMutableArray *animFrames = [NSMutableArray array];
        for( int i=1;i<=5;i++)
        {
            NSString* file = [NSString stringWithFormat:@"dog%d.gif", i];
            CCTexture2D* texture = [[CCTextureCache sharedTextureCache] addImage:file];
            CGSize texSize = texture.contentSize;
            CGRect texRect = CGRectMake(0, 0, texSize.width, texSize.height);
            CCSpriteFrame* frame = [CCSpriteFrame frameWithTexture:texture rect:texRect];
            [animFrames addObject:frame];
        }
        CCAnimation * animation = [CCAnimation animationWithSpriteFrames:animFrames];
        animation.delayPerUnit = 0.07f;
        animation.restoreOriginalFrame = YES;

        CCAnimate *animAction  = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation]];
        [dog runAction:animAction];

Above code is just describe that how can you add animated CCSprite without use of texturepacker

Here you can also change array of Images so you may be add manually image also.

iPatel
  • 46,010
  • 16
  • 115
  • 137
0

When you create a CCSpriteBatchNode it is linked to a single texture. This is the point of a CCSpriteBatchNode: to draw different sprites that use the same texture to reduce OpenGL draw calls and increase efficiency.

The easiest workaround (if you have not reached a performance-critical point) would be to use a regular CCLayer instead of a CCSpriteBatchNode.

If you still want to add different CCSprites (say, the body, the limbs and the head of your character) to the same CCSpriteBatchNode, the you need to build a single sprite sheet (or texture pack) which contains all the body parts that you need to add to the CCSpriteBatchNode. This single sprite sheet will be the only one that the CCSpriteBatchNode will use. You won't be able to add CCSprites that are not using this sprite sheet.

Ricardo Sanchez-Saez
  • 9,466
  • 8
  • 53
  • 92