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.
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.
我想你也可以这样做:
SKSpriteNode* currentSprite = [SKSpriteNode spriteNodeWithTexture:[_arrayImg objectAtIndex:1]];
[currentSprite setName:[NSString stringWithFormat:@"%@", [_arrayImg objectAtIndex:1]]];
然后找到 SKSpriteNode 做,
SKSpriteNode* currentSprite = (SKSpriteNode*)[self childNodeWithName:[NSString stringWithFormat:@"%@", [_arrayImg objectAtIndex:1]]]
或找出 SKSpriteNode 的图像名称,
for (SKNode* node in self.children) {
if ([node isKindOfClass:SKSpriteNode.class]) {
SKSpriteNode* sprite = (SKSpriteNode*)node;
NSString* name = sprite.name;
}
}
您必须“记住”它,例如在 userData 中。
NSString* imageName = [_arrayImg objectAtIndex:1];
SKSpriteNode *currentSprite = [SKSpriteNode spriteNodeWithImageNamed:imageName];
currentSprite.userData = [NSMutableDictionary dictionaryWithObject:imageName
forKey:@"imageName"];