access child nodes in spritekit
ios, objective-c, sprite-kit
Solution
Try this:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:positionInScene];
}
You can also set name of your node:
[sprite setName:@"NodeName"];
And you can access it by name:
[self childNodeWithName:@"NodeName"];
Problem
Hi all i'm new in spriteKit and objective-c and I would like to create a spriteNode in a method and remove it in another method (same .m file) In this method i create the sprite: ``` (void)createSceneContents{ /*in this method i create and add the spaceship spriteNode SKSpriteNode *spaceship = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"]; //code... //Add Node [self addChild:spaceship]; } ``` And now i would like to remove the node touching it, but I only know the method for handle touch events is: ``` -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event ``` I'm trying to access my spaceship node from there i can't. I've tried everything without success. IS there a way to send a node from a method to another?? Or without sending it, is it posible to access to a children node from a method where it's not declared??