Can I get the image name from SKSpriteNode?

We can assign an image to SKSpriteNode using code

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

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

+6
source share
2 answers

I suppose you could do this too:

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

then find SKSpriteNode do,

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

or find out the name of the image SKSpriteNode do,

 for (SKNode* node in self.children) { if ([node isKindOfClass:SKSpriteNode.class]) { SKSpriteNode* sprite = (SKSpriteNode*)node; NSString* name = sprite.name; } } 
+2
source

You will 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"]; 
+1
source

Source: https://habr.com/ru/post/957493/


All Articles