ExceptionSlayer ExceptionSlayer - 1 year ago 97
Objective-C Question

getting a cocos2d sprite image path

How could I get the image file path of a sprite in string form in cocos2d?

Are there any classes or methods that specifically do this? The easiest and simplest way would be appreciated.

Answer Source

You must get texture of sprite (CCTexture2D or CCTextureAtlas). It would be value for some string key in CCTextureCache textures. This key is your path.

As I see in Cocos2d, CCTextureCache has no property/method to access its textures, so you need to expand this (for example in category):

@interface CCTextureCache (getTexturePath)
-(NSString*) getTexturePath: (Texture2D*) texture;

@implementation CCTextureCache (getTexturePath)
-(NSString*) getTexturePath: (Texture2D*) texture
    return [[[textures allKeysForObject: texture] lastObject];


So, to get path for some sprite:

NSString* path = [[CCTextureCache sharedTextureCache] getTexturePath: [mySprite texture]];