Tarek
Tarek

Reputation: 11

Changing sprite texture from a CCSpriteBatchNode

I am trying to change the texture of a sprite that I create from a SpriteBatchNode.

[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA4444];

spritesBgNode = [CCSpriteBatchNode batchNodeWithFile:@"playingCards.pvr.ccz"];
[self addChild:spritesBgNode];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"playingCards.plist"];

I searched and found examples that use

Texture2D *texture = [[Texture2D alloc] initWithImage:myUIImage]

[sprite setTexture: texture];

So my questions is: how do I get the image from my batchNode file? Or do I use another method to get a reference to the image in my playingCards.pvr.ccz file.

UPDATE

First off thanks for the response. So I have mySprite with the image of a King using the code snippet you provided. But I want to change the sprite's texture to display the back of the card (so it can be played face up or down) I have both images inside CCSpriteBatchNode.

But as you point out "You can't get the image from a batchNode", so I can't use [[Texture2D alloc] initWithImage:myUIImage]

So do I go about changing the sprite's image from face up to face down.

Thanks

Upvotes: 1

Views: 2851

Answers (1)

nash
nash

Reputation: 2181

If you want to display the images in your .pvr.ccz file to the screen then add the following code:

CCSprite * mySprite = [CCSprite spriteWithSpriteFrameName: @"name of sprite frame"];
[spritesBgNode addChild: mySprite];

Basically, to display parts of your batchNode, you need to add a sprite to it. The name of the sprite frame is in the .plist file you added to the FrameCache.

You can't get the image from a batchNode. UIImage is the iPhone API type of image, not cocos2d. In cocos2d, initWithImage:(UIImage*)image is provided for convenience.

If you use [[Texture2D alloc] initWithImage:myUIImage], the UIImage is used to create an NSData object, and [texture initWithData: data] is called internally. The image isn't stored for later use.

Update

The sprite works as a 'view to a batchNode' in this case. To view a different part of the batch node, change the frame of your sprite.

[mySprite setDisplayFrame:
    [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: @"back of card"]]];

Upvotes: 1

Related Questions