Search code examples
iosswiftsprite-kitskscene

Calling SKScene method from GameViewController


While in the GameViewController, I want to access methods from one of my SKScenes. Its class is called PlayScene.

I have been trying to work it out, but haven't managed it. I'm trying things like let gameScene = self.something.something as! PlayScene

I also tried using let scene = SKScene(fileNamed: "GameScene") but then when I tried to call the method it said something like "unresolved identifier." I use this when I present this scene, so I thought it would work.

Am I close? How can I access my SKScene class from GameViewController?


Solution

  • You just need to cast the SKScene as a PlayScene.

    let playScene = SKScene(fileNamed: "GameScene") as? PlayScene
    

    Now the compiler knows that your SkScene is also a PlayScene, and you can call PlayScene methods on it.