访问spritekit中的子节点

Alf*_*fro 4 objective-c ios sprite-kit

大家好我是spriteKit和objective-c的新手,我想在方法中创建一个spriteNode并在另一个方法中删除它(相同的.m文件)在这个方法中我创建了一个精灵:

(void)createSceneContents{ /*in this method i create and add the spaceship spriteNode
        SKSpriteNode *spaceship = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];

        //code...

        //Add Node
        [self addChild:spaceship];
    }
Run Code Online (Sandbox Code Playgroud)

现在我想删除触摸它的节点,但我只知道处理触摸事件的方法是:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
Run Code Online (Sandbox Code Playgroud)

我正试图从那里访问我的宇宙飞船节点我不能.我尝试了一切都没有成功.有没有办法将节点从一个方法发送到另一个?或者不发送它,是否可以从未声明的方法访问子节点?

Gre*_*reg 8

试试这个:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:positionInScene];
}
Run Code Online (Sandbox Code Playgroud)

您还可以设置节点的名称:

[sprite setName:@"NodeName"];
Run Code Online (Sandbox Code Playgroud)

您可以通过名称访问它:

[self childNodeWithName:@"NodeName"];
Run Code Online (Sandbox Code Playgroud)