SpriteKit - didMovetoView 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 GameScene

import SpriteKit
import GameplayKit

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

But for some reason, in the debugging area, he did not print β€œreceives a call,” indicating that didMove did not even call the call. What's going on here? Did I miss something?

+4
source share
5 answers

I finally sort it.

The name of my project contains a period at the end. Like the name "XXXX.". After several experiments, I found that I can simply solve the problem by deleting the period.

+1
source

IOS 9 method

In your GameViewController, try directly presenting your GameScene instead of a generic SKScene.

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

, "fileNamed" - .swift, .sks, xCode.

iOS 10

, Apple SKScene, .

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

, .sks . ( ) .swift .

, .

+6

, SKS (GameScene.sks ) , , / - .

+2

:

override func didMoveToView(view: SKView)

override func didMove(to view: SKView)

, /API...

0

I am the second that said Tom Xue, as a person who too long sought the answer to this question. I had a hyphen in the name of my application and this seemed to be causing the problem. When I renamed my project, the scene was presented as it should have been.

-1
source

Source: https://habr.com/ru/post/1657026/


All Articles