Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpriteKit - didMovetoView is not called

This is my code for the view controller

import UIKit
import SpriteKit
import GameplayKit
class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    if let view = self.view as! SKView? {
        // Load the SKScene from 'GameScene.sks'
        if let scene = SKScene(fileNamed: "GameScene") {
            // Set the scale mode to scale to fit the window
            scene.scaleMode = .aspectFill
            // Present the scene
            view.presentScene(scene)
        }

        view.ignoresSiblingOrder = true

        view.showsFPS = true
        view.showsNodeCount = true
    }
}

And this is my code for the GameScene

import SpriteKit
import GameplayKit

    class GameScene: SKScene {
       override func didMove(to view: SKView) {
            print("gets called")
        }

But for some reasons, in the debug area, it didn't print "gets called", which indicates that didMove didn't even get called. What's happening here? Did I miss anything?

like image 786
Tom Xue Avatar asked Oct 07 '16 07:10

Tom Xue


1 Answers

The iOS 9 way

In your GameViewController try to directly present your GameScene instead of a generic SKScene.

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

Remember "fileNamed" is not the name of the .swift file, its the name of the .sks file which is used for the xCode level editor.

The new iOS 10 way

It seems Apple now prefers to pass a generic SKScene like you are trying.

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

To make it work go to the relevant .sks file and go to the inspector on the right. Click the second last item (custom class) and enter the name of the .swift file into the custom class field.

Hope this helps.

like image 108
crashoverride777 Avatar answered Sep 16 '22 11:09

crashoverride777