无法让应用程序进入主菜单(Swift 4 SpriteKit)

时间:2018-06-13 23:00:30

标签: ios swift sprite-kit swift4

我正在尝试在游戏加载后立即将用户引导到主菜单文件(使用Swift 4 SpriteKit制作)。但是,当我尝试将用户重定向到此.swift文件(其中标签应显示在用户屏幕上)时,除了节点和FPS计数器之外,屏幕只是空白。

这是我创建的MainMenuScene.swift文件中的代码:

import Foundation
import SpriteKit

class MainMenu: SKScene {
    override func didMove(to view: SKView) {
        print("In scene")
        let myLabel = SKLabelNode(fontNamed: "The Bold Font")
        myLabel.fontColor = SKColor.blue
        myLabel.text = "My Label"
        myLabel.fontSize = 50
        myLabel.position = CGPoint(x: 0, y: 0)
        myLabel.zPosition = 1
        self.addChild(myLabel)
    }


}

这是我在GameViewController.swift中的代码

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: "MainMenuScene") {
                // 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
            view.showsPhysics = true
        }
    }

    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
    }
}

我改变了

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

if let scene = SKScene(fileNamed: "MainMenuScene")

这行代码应该将用户重定向到MainMenuScene.swift,但是当我运行程序时,屏幕只是空白。 谁知道我做错了什么? 谢谢!

1 个答案:

答案 0 :(得分:-1)

之后尝试使用以下代码
    super.viewDidLoad()

另外,请确保您的Menu类名为“MainMenuScene”,并且您在MainMenuScene.sks enter image description here

中将场景名称更改为相同

CODE:

 if let scene = GKScene(fileNamed: "MainMenuScene") {

        // Get the SKScene from the loaded GKScene
        if let sceneNode = scene.rootNode as! MainMenuScene? {

           scene.scaleMode = .aspectFill

            // Present the scene
            if let view = self.view as! SKView? {


                view.presentScene(sceneNode)

                view.ignoresSiblingOrder = true

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