Pregunta

I would like to get name of the image which I use on SKSpriteNode.

This is how I create a node:

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

By touching the screen I would like to get on NSLog the name of this image.

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

By touching node, I would like to get on NSLog information that this SKSpriteNode uses image named img.png.

How I should solve this problem?

Thanks in advance.

¿Fue útil?

Solución

You don't have direct access to the name of the image. A possible solution is to use the name property of the node for it.

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

That way you'd have name by simply printing the description of the object.

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

Otros consejos

I ran across this same question today and found an exact solution. If you would like to access the image of your SKSpriteNode, try:

NSLog(@"Texture: %@",[button texture]);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top