Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

access child nodes in spritekit

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??

like image 848
Alfro Avatar asked Dec 25 '22 17:12

Alfro


1 Answers

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"];
like image 81
Greg Avatar answered Dec 28 '22 10:12

Greg