0

So I am developing an AR application. I have a button that initiates a exam/test mode where use has to answer questions correctly and gains one mark for each correct answer. When the test mode is initiated a pointOfView node from the defaultCameraController is used and the score is displayed on the ARSCN. Ending the test removes the node from the screen. However, after I click on the test button again in the same session, the code gets called, however it does not appear on the screen. The code for the start test button, stop test button and the setup scoreboard function are shown below. Excuse the clutter. I have to organise the code after fixing the functionality.

   @IBAction func testButton(_ sender: Any) {
        if sceneView.scene.rootNode.childNodes.count > 0{
        testScore = 0
        learningModeEnabled = false
        learningMode.isEnabled = false
        for nodePos in nodePositions{
            // Create 3D Text


        }
        startTestButton.isHidden = true
        stopTestButton.isHidden = false
    //--------------------------------------------------------------------------

            setupScoreboard()}

        else{
            print("No Nodes Detected in Environment")
        }

    }


    @IBOutlet weak var stopTestButton: PressableButton!
    @IBAction func stopTestButton(_ sender: Any) {
        nodePositions = [:]
        sceneView.scene.rootNode.enumerateChildNodes { (node, stop) in
            node.removeFromParentNode()


        }
        stopTestButton.isHidden = true
        startTestButton.isHidden = false

        let userDefaults = UserDefaults.standard
        userDefaults.setValue(testScore, forKey: "score")
        userDefaults.synchronize()
    //--------------------------------------------------------------------------

self.sceneView.defaultCameraController.pointOfView?.enumerateChildNodes({ (node, stop) in
            node.removeFromParentNode()
        })

        if userDefaults.data(forKey: "score") != nil{
        userScores = userDefaults.array(forKey: "score") as! [Int]
        print(userScores)
        }

    }



    //--------------------------------------------------------------------------


    func setupScoreboard(){

        self.sceneView.defaultCameraController.pointOfView?.enumerateChildNodes({ (node, stop) in
            node.removeFromParentNode()
        })
            let worldCoord : SCNVector3 =  (self.sceneView.defaultCameraController.pointOfView?.position)!


            let node : SCNNode = self.createNewBubbleParentNode("Score: \(String(testScore))")
            node.position = SCNVector3( 0, 0.3, -1)

            node.name = "Scoreboard"
            self.sceneView.defaultCameraController.pointOfView?.addChildNode(node)


    }

Edit: Added Comment lines near code relevant to question.

mcma
  • 21
  • 5
  • The way you have added your code here, doesn't clarify in what sequence are the functions being called. In addition, because of indentation issues, it is difficult to make out where scope of one method ends and another begins. If you could format it better, we could take a better dig at it. In general, before pressing your test button, are you sure there are `childnodes` within `pointOfView`? I am not able to make this out clearly. – Manganese Jun 23 '18 at 17:04
  • @Manganese Sorry. Removed most of the useless code now. Once again, the testButton is pressed first. The setupScoreboard() function is run and the score 3d text appears sucessfully. After that I press the stopTestButton where the score gets saved and the node in pointOfView disappears cz i remove it. Then again i press the testButton and everything else in that button works as usual, but the node just isnt added. When checking through debugger " self.sceneView.defaultCameraController.pointOfView?.addChildNode(node) " is run successfully. Yet it does not show up on AR scene. – mcma Jun 23 '18 at 17:34
  • Your `pointOfView` moves with you. It is attached to your camera. If you add your node, without positioning it in front of you, e.g. `node.position.z = 2.0` (or try `-2.0`) you would not be able to see it. Rather, try adding node as child nodes of `rootnode` of your `ARSCNView`. That way it should remain static even if your camera position changes. Test these combination and let me know whether it works. – Manganese Jun 23 '18 at 17:38
  • @Manganese I actually want it to move with the camera. Using " node.position = SCNVector3( 0, 0.3, -1)" in setupScoreboard() works exactly as I want it to. It moves around with the camera so the score is always visible to the user while moving around. The issue is when adding the same scoreboard node into the scene the second time. It works the first time I use the exam/test feature. But when I start the test again, the scoreboard node doesn't appear in front of me as it did the first time. – mcma Jun 23 '18 at 18:08
  • Consider `if sceneView.scene.rootNode.childNodes.count > 0 {}` logic carefully before checking why it is working the first time and not any other time. `pointOfView` and `rootNode` are different things. – Manganese Jun 23 '18 at 18:46
  • As per your logic, only when rootNode has a childNode, scoreboard is added to the pointOfView.I wonder, why in the first place you are having the logic: `if sceneView.scene.rootNode.childNodes.count > 0 {}` for setting up your scoreboard. And about, why it is working the first time? Probably during your `viewDidLoad()`, etc. methods, you are adding a child node to root node, which is why that logic works the first time. After that on pressing *stopTest* button, it removes every node from your root node. That's why setUpScoreboard is not working. – Manganese Jun 23 '18 at 18:52
  • @Manganese Well, the childNodes of the rootNode are the questions. So there must be questions present for the exam mode to be turned on. If there is no childNodes in the rootNode, print("No Nodes Detected in Environment") this line is run. And if there is, it continues to run the setupScoreboard function. So AFAIK, the code gets executed the way it is supposed to. But the results of the 2nd execution arent as expected. Let me just test if there are any nodes added to pointOfView when the setupScoreboard is run for the second time. – mcma Jun 24 '18 at 02:30
  • @Manganese I can confirm that the node gets added to pointOfView successfully when it is run the second time. It just does not show up on the screen. When I ran "print("pointOfView childNodeCount = ", self.sceneView.defaultCameraController.pointOfView?.childNodes.count)", it gave me the result of pointOfView childNodeCount = Optional(1) pointOfView childNodeCount = Optional(1) both the times I started the exam. So this confirms that the node is successfully added to pointOfView both time. – mcma Jun 24 '18 at 02:38
  • My apologies man, but I am not able to figure out what's wrong then. Hopefully someone else would be. Thanks. – Manganese Jun 25 '18 at 16:13
  • Got the same issue. When I remove and add to scene.rootnode it is adding agian and again but not for pointofview. ANyone else facing this issue and solved? – Rakesh iOS Dev Sep 19 '18 at 09:25

0 Answers0