How to call a GameViewController function form SKScene which is overlaid

40 views Asked by At

I have: GameViewController, where I setup a 3D scene and a function "fire". Then I have another SKScene, which is overlaid, so I cannot click the nodes on the 3D scene. On the SKScene I have a SKSpriteNode, "fireNode". After click of this fireNode I want to call a "fire "function in GameViewCOntroller. How can I do this?

1

There are 1 answers

1
Stamenkovski On

If your GameViewController is rootViewController I would go with something like this:

func fire() {
   guard let keyWindow = UIApplication.shared.windows.first(where: { $0.isKeyWindow })
   else { return }
        
   if let rootViewController = keyWindow.rootViewController as? GameViewController {
      rootViewController.onFire()
   }
 }

Edit:
Or you can just post notification with Notification Center to your view controller.