How to remove SCNNode when Tap on it?

ぐ巨炮叔叔 提交于 2019-12-24 10:55:25

问题


I have Creat Cubes using SceneView and i want to disappear cube on which Tap action. How can achieve it?

Here is my code to create 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];
   } 
 }

but I'm not able to remove Node from Tap action. Can you please help me, and give better suggestions, thank you... :)


回答1:


You need remove the node you are touching using [hitNode removeFromParentNode];

Code

- (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];
}


来源:https://stackoverflow.com/questions/47977468/how-to-remove-scnnode-when-tap-on-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!