0
votes

can you create a new CCSprite from a part of a CCSprite within a CCSpriteBatchNode?

For a long time, I've used SpriteFrameCache and BatchNode without a 100% understanding of the two, in particular how they relate to the textureCache. I could use some clear advice to accomplish the following:

Currently, I load a texture atlas into CCSpriteBatchNode and the frame list into the CCSpriteFrameCache and generate a sprite in what I think is basic standard fasion

CCSpriteBatchNode *batchNode = [CCSpriteBatchNode batchNodeWithFile:@"textureAtlasImage.png"];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"textureAtlasFrames.plist"];

CCSprite * gameObject = [CCSprite spriteWithSpriteFrameName:@"gameObject.png"];
[self addChild:batchNode];
[batchNodeaddChild:gameObject];

For the sake of simplicity with the question, What I'd like to do is divide gameObject into 4 pieces programatically (rather than divide the original image into four pieces and adding each into the textureAtlasImage.png individually).

From reading, I'm thinking something like:

CCSpriteFrame * gameObjectFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"textureAtlasFrames.plist"];
CCTexture2D * gameObjectIndividualTexture = [[gameObjectFrame] texture];
CCSpriteFrame * gameObjectPartFrame = [CCSpriteFrame frameWithTexture:gameObjectIndividualTexture offset: rectInPixels: ] ;
[[CCSpriteFrameCache sharedSpriteFrameCache]  addSpriteFrame:gameObjectPartFrame name:@"gameObjectPart1"];

But my questions then are: Is this already in the batchNode? If not, how do I actually create the sprite out of the gameObjectPart1 using batchNodes? Is it wasteful to add another spriteframe to the cache that duplicates data elsewhere?

1

1 Answers

1
votes

You should be able to adjust the texture rect after creating the sprite. Create four sprites using the same sprite frame, then set each sprite's texture rect to one of the 4 smaller regions you want them to use.

Use the sprite.textureRect property to get the CGRect with the original size and change the size respectively origin. For example the lower left rectangle can be created by setting textureRect to the same rect but with size.width and size.height halved.