gpt4 book ai didi

ios - 在 spritekit 中访问子节点

转载 作者:可可西里 更新时间:2023-11-01 05:05:47 24 4
gpt4 key购买 nike

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

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

//code...

//Add Node
[self addChild:spaceship];
}

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

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

我正在尝试从那里访问我的宇宙飞船节点,但我不能。我已经尝试了一切但没有成功。有没有办法将节点从一种方法发送到另一种方法?或者不发送,是吗可以从未声明的方法访问子节点??

最佳答案

试试这个:

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

您还可以设置节点名称:

[sprite setName:@"NodeName"];

您可以通过名称访问它:

[self childNodeWithName:@"NodeName"];

关于ios - 在 spritekit 中访问子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20200749/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com