6

We can assign image in SKSpriteNode using the code

SKSpriteNode *currentSprite = [SKSpriteNode spriteNodeWithTexture:[_arrayImg objectAtIndex:1]];

But how can I get the image name from the SKSpriteNode currentSprite.

Banshidhari
  • 478
  • 2
  • 12

2 Answers2

2

I suppose you could also do this:

SKSpriteNode* currentSprite = [SKSpriteNode spriteNodeWithTexture:[_arrayImg objectAtIndex:1]];
[currentSprite setName:[NSString stringWithFormat:@"%@", [_arrayImg objectAtIndex:1]]];

then finding the SKSpriteNode do,

SKSpriteNode* currentSprite = (SKSpriteNode*)[self childNodeWithName:[NSString stringWithFormat:@"%@", [_arrayImg objectAtIndex:1]]]

or finding out the image name of the SKSpriteNode do,

for (SKNode* node in self.children) {
    if ([node isKindOfClass:SKSpriteNode.class]) {
        SKSpriteNode* sprite = (SKSpriteNode*)node;
        NSString* name = sprite.name;
    }
}
John Riselvato
  • 12,854
  • 5
  • 62
  • 89
1

You'll have to "remember" it, for example in userData.

NSString* imageName = [_arrayImg objectAtIndex:1];
SKSpriteNode *currentSprite = [SKSpriteNode spriteNodeWithImageNamed:imageName];
currentSprite.userData = [NSMutableDictionary dictionaryWithObject:imageName 
                                                            forKey:@"imageName"];
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • Actually the array image is not the array of images at first I wrote the question wrong. The array images are array of SKTexture. So how can i get the image name. – Banshidhari Nov 06 '13 at 11:19
  • Store the textures in a dictionary with keys being the image name. – CodeSmile Nov 06 '13 at 11:31