How do I pop views from inside the GameScene class?

I am a noob to swift and programming in general and i am trying to make my first app.


I have made it using a navigation controller which has a root view controller (MainViewController.swift). on this view controller i have a button which pushes the game view controller on to the scene:



@IBAction func moveToGame(_ sender: Any) {

let gameVC = self.storyboard?.instantiateViewController(withIdentifier: "gameVC") as! GameViewController

navigationController?.pushViewController(gameVC, animated: true)

}



I have then made the GameScene class conform to the SKPhysicsContactDelegate protocol and sat up this function inside the GameScene class:


func didBegin(_ contact: SKPhysicsContact) {

if contact.bodyA.node?.name == "asteroide" || contact.bodyA.node?.name == "starman" {


}

}


what i then want to do, is to move back to the first view controller when this function is called. I first tried the function:


navigationController?.popViewController(animated: true)


but this doesn't seem to work inside the GameScene class. (It works when i put it inside GameViewController)


My question is. How do i pop this view of the navigation controller so that i get back to the first view controller (the one with the button)?

How do I pop views from inside the GameScene class?
 
 
Q