我们可以使用代码
在SKSpriteNode中分配图像SKSpriteNode *currentSprite = [SKSpriteNode spriteNodeWithTexture:[_arrayImg objectAtIndex:1]];
但是如何从SKSpriteNode currentSprite 获取图像名称。
答案 0 :(得分:2)
我想你也可以这样做:
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;
}
}
答案 1 :(得分:1)
你必须“记住”它,例如在userData中。
NSString* imageName = [_arrayImg objectAtIndex:1];
SKSpriteNode *currentSprite = [SKSpriteNode spriteNodeWithImageNamed:imageName];
currentSprite.userData = [NSMutableDictionary dictionaryWithObject:imageName
forKey:@"imageName"];