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??
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"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With