Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can we get image name from SKSpriteNode?

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.

like image 336
Banshidhari Avatar asked Nov 06 '13 11:11

Banshidhari


2 Answers

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;
    }
}
like image 145
John Riselvato Avatar answered Oct 19 '22 10:10

John Riselvato


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"];
like image 1
LearnCocos2D Avatar answered Oct 19 '22 08:10

LearnCocos2D