Tom
Tom

Reputation: 47

How to convert a CCSpriteFrame to a CCTexture2D (Cocos2d)

Is it possible to convert a CCSpriteFrame that was taken from CCSpriteFrameCache, and convert it into a texture that can be set on a sprite texture property?

Upvotes: 0

Views: 2819

Answers (2)

CodeSmile
CodeSmile

Reputation: 64477

Assuming your CCSpriteFrame is named frame you can use:

[sprite setDisplayFrame:frame];

to change the sprite's frame if it uses the same texture. If the texture is not the same, you must create a new sprite:

CCSprite* sprite = [CCSprite spriteWithTexture:frame.texture];
[sprite setDisplayFrame:frame];

But since you already have the CCSpriteFrame you can just as well call initWithFrame:

CCSprite* sprite = [CCSprite spriteWithSpriteFrame:frame];

Upvotes: 2

Saturn
Saturn

Reputation: 18149

Yes, it has a texture function:

[[[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"MyFrame"]texture];

CCSprites have a method called setTexture or something.

Upvotes: 0

Related Questions