如何在点击 SCNNode 时删除它?
How to remove SCNNode when Tap on it?
我有使用 SceneView 的 Creat Cubes,我想在点击操作时消失立方体。怎样才能实现呢?
这是我创建 Cube 的代码
SCNBox *Box = [SCNBox boxWithWidth:2.0 height:2.0 length:2.0
chamferRadius:Radius];
Box.firstMaterial.diffuse.contents = [UIColor whiteColor];
SCNNode *cubeNode = [SCNNode nodeWithGeometry:Box];
[ArrBoxNode addObject:cubeNode];
self.sceneView.backgroundColor = [UIColor redColor];
self.view.backgroundColor = [UIColor grayColor];
cubeNode.position = SCNVector3Make(4,0,0);
[scene.rootNode addChildNode:cubeNode];
self.sceneView.scene = scene;
[self.sceneView sizeToFit];
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.sceneView];
SCNHitTestResult *hitTestResult = [[self.sceneView
hitTest:touchPoint options:nil] firstObject];
SCNNode *hitNode = hitTestResult.node;
for (SCNNode *node in ArrBoxNode) {
[node removeFromParentNode];
}
}
但我无法从点击操作中删除节点。能不能帮帮我,给点更好的建议,谢谢...:)
您需要使用 [hitNode removeFromParentNode];
删除您正在触摸的节点
代码
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.sceneView];
SCNHitTestResult *hitTestResult = [[self.sceneView hitTest:touchPoint options:nil] firstObject];
SCNNode *hitNode = hitTestResult.node;
[hitNode removeFromParentNode];
}
我有使用 SceneView 的 Creat Cubes,我想在点击操作时消失立方体。怎样才能实现呢?
这是我创建 Cube 的代码
SCNBox *Box = [SCNBox boxWithWidth:2.0 height:2.0 length:2.0
chamferRadius:Radius];
Box.firstMaterial.diffuse.contents = [UIColor whiteColor];
SCNNode *cubeNode = [SCNNode nodeWithGeometry:Box];
[ArrBoxNode addObject:cubeNode];
self.sceneView.backgroundColor = [UIColor redColor];
self.view.backgroundColor = [UIColor grayColor];
cubeNode.position = SCNVector3Make(4,0,0);
[scene.rootNode addChildNode:cubeNode];
self.sceneView.scene = scene;
[self.sceneView sizeToFit];
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.sceneView];
SCNHitTestResult *hitTestResult = [[self.sceneView
hitTest:touchPoint options:nil] firstObject];
SCNNode *hitNode = hitTestResult.node;
for (SCNNode *node in ArrBoxNode) {
[node removeFromParentNode];
}
}
但我无法从点击操作中删除节点。能不能帮帮我,给点更好的建议,谢谢...:)
您需要使用 [hitNode removeFromParentNode];
代码
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.sceneView];
SCNHitTestResult *hitTestResult = [[self.sceneView hitTest:touchPoint options:nil] firstObject];
SCNNode *hitNode = hitTestResult.node;
[hitNode removeFromParentNode];
}