How to use protocol properly to call a GameViewController's function?

145 views Asked by At

I have a GameScene.sks, GameScene class and GameViewController. I can create a protocol without using sks but I want to do this using SKScene(fileNamed: "GameScene").

When I use scene.gameDelegate = self in GameViewController, viewDidload() I'm getting Value of type 'SKScene' has no member gameDelegate error.

How Can I overcome this problem?

protocol GameDelegate {

    func createAds()
}

GameScene class:

class GameScene: SKScene {

    var gameDelegate: GameDelegate!

    ...

GameViewController viewDidLoad:

        if let view = self.view as! SKView? {

            if let scene = SKScene(fileNamed: "GameScene") {

                scene.scaleMode = .aspectFill
                scene.gameDelegate = self // Value of type 'SKScene' has no member gameDelegate

                gameState = .playing
                view.presentScene(scene)
            }

            view.ignoresSiblingOrder = true

        }
1

There are 1 answers

2
Jonathan H. On BEST ANSWER

Initialize the local variable scene as your GameScene subclass of SKScene. This would look something like:

        if let view = self.view as! SKView? {

        // change type of optionally bound constant 'scene'
        if let scene = GameScene(fileNamed: "GameScene") {

            scene.scaleMode = .aspectFill
            scene.gameDelegate = self 

            gameState = .playing
            view.presentScene(scene)
        }

        view.ignoresSiblingOrder = true

    }

Also ensure that the GameViewController class conforms to the GameDelegate protocol and implements all of the required interface methods.