当以编程方式完成时,不会在SpriteKit中加载任何内容

时间:2021-11-01 15:16:02

I can't figure out why this won't work, and it doesn't seem as if the scene is changing because the background colour does not change to red. Here is my code:

我搞不懂为什么这个不能用,而且看起来场景好像没有变,因为背景颜色没有变红。这是我的代码:

GameViewController.swift

GameViewController.swift

class GameViewController: UIViewController {

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        let skView = self.view as! SKView
        if skView.scene == nil {

            // Set options
            skView.ignoresSiblingOrder = true
            skView.showsFPS = true
            skView.showsNodeCount = true
            skView.showsPhysics = true

            // Create a scene
            let gameScene = SKScene(size: skView.bounds.size)

            // Set the scale mode to scale to fit the window
            gameScene.scaleMode = .aspectFill

            // Present the scene
            skView.presentScene(gameScene)
        }     
    }

    override var shouldAutorotate: Bool {
        return true
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        if UIDevice.current.userInterfaceIdiom == .phone {
            return .allButUpsideDown
        } else {
            return .all
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Release any cached data, images, etc that aren't in use.
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }
}

GameScene.swift

GameScene.swift

class GameScene: SKScene {

    // Create objects
    var player = SKSpriteNode()

    // Initialize
    override init(size: CGSize) {
        super.init(size: size)

        //Other init code
        player.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
        player.setScale(0.5) 
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func didMove(to view: SKView) {
        self.backgroundColor = UIColor.red
        self.addChild(player)  
    }  
}

Nothing loads onto the screen, it is just black/grey. Any ideas?

没有加载到屏幕上,只是黑色/灰色。什么好主意吗?

1 个解决方案

#1


1  

No where in your code are you specifying that you want to open GameScene.

在您的代码中没有指定要打开GameScene的地方。

// Create a scene
let gameScene = SKScene(size: skView.bounds.size)

if you do not have a corresponding SKS file created in the scene editor but would rather load the scene that was created in code use...

如果您没有在场景编辑器中创建相应的SKS文件,但更希望加载在代码使用中创建的场景……

what it should be...

它应该是……

// Create a scene
let gameScene = GameScene(size: skView.bounds.size)

#1


1  

No where in your code are you specifying that you want to open GameScene.

在您的代码中没有指定要打开GameScene的地方。

// Create a scene
let gameScene = SKScene(size: skView.bounds.size)

if you do not have a corresponding SKS file created in the scene editor but would rather load the scene that was created in code use...

如果您没有在场景编辑器中创建相应的SKS文件,但更希望加载在代码使用中创建的场景……

what it should be...

它应该是……

// Create a scene
let gameScene = GameScene(size: skView.bounds.size)