2014-03-29 291 views
2

我想获得我在SKSpriteNode上使用的图像的名称。如何从SKSpriteNode获取图像名称?

这是我如何创建一个节点:

SKSpriteNode *button = [SKSpriteNode spriteNodeWithImageNamed:@"img.png"]; 

通过触摸我想获得的NSLog这一形象的名称的屏幕。

UITouch *touch = [touches anyObject]; 
CGPoint loc = [touch locationInNode:self]; 
SKSpriteNode *node = (SKSpriteNode *)[self nodeAtPoint:loc]; 

通过触摸节点,我想获得NSLog信息,该SKSpriteNode使用名为img.png的图像。

我该如何解决这个问题?

在此先感谢。

回答

7

您无法直接访问图像的名称。一个可能的解决方案是使用该节点的name属性。

NSString *filename = @"img.png"; 
SKSpriteNode *button = [SKSpriteNode spriteNodeWithImageNamed:filename]; 
[button setName:filename]; 

这样你就可以通过简单地打印对象的描述来获得名称。

NSLog("Touched object %@", [node description]); 
2

我今天跑过这个相同的问题,发现一个确切的解决方案。如果您想访问您的SKSpriteNode的图像,请尝试:

NSLog(@"Texture: %@",[button texture]);