我試圖用2個按鈕作爲初始場景來呈現菜單:play
,credits
。當單擊play
時,我想用4個按鈕呈現另一個菜單場景:tutorial
,easy
,hard
,back
。
該策略只是將按鈕創建爲SKSpriteNode
對象並處理touchesBegan
函數中的點擊。
在我的menuScene.sks
文件中,我正確放置並命名了我的節點。在這裏,你可以檢查menuScene.swift
文件鏈接到了這一幕:SpriteKit:解開時意外找到零,無法實現多個菜單場景
import SpriteKit
class menuScene: SKScene {
var playButton:SKSpriteNode!
var creditsButton:SKSpriteNode!
override func didMove(to view: SKView) {
playButton = self.childNode(withName: "playButton") as! SKSpriteNode
creditsButton = self.childNode(withName: "creditsButton") as! SKSpriteNode
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// transition is defined in Helper.swift
let touch = touches.first
if let location = touch?.location(in: self){
let nodesArray = self.nodes(at: location)
if nodesArray.first?.name == "playButton" {
let nextScene = difScene(size: self.size)
self.view?.presentScene(nextScene, transition: transition)
} else if nodesArray.first?.name == "creditsButton" {
let nextScene = creditsScene(size: self.size)
self.view?.presentScene(nextScene, transition: transition)
}
}
}
}
當我運行,menuScene呈現沒有問題。但是,當我觸摸play
按鈕時,出現錯誤,它來自下一個場景:difScene
。 在這裏你可以找到difScene.swift
:
import SpriteKit
class difScene: SKScene {
var tutButton:SKSpriteNode!
var easyButton:SKSpriteNode!
var hardButton:SKSpriteNode!
var backButton:SKSpriteNode!
override func didMove(to view: SKView) {
tutButton = self.childNode(withName: "tutButton") as! SKSpriteNode // error
easyButton = self.childNode(withName: "easyButton") as! SKSpriteNode
hardButton = self.childNode(withName: "hardButton") as! SKSpriteNode
backButton = self.childNode(withName: "backButton") as! SKSpriteNode
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let transition = SKTransition.push(with: .down, duration: 0.2)
let touch = touches.first
if let location = touch?.location(in: self){
let nodesArray = self.nodes(at: location)
if nodesArray.first?.name == "tutButton" {
let nextScene = GameScene(size: self.size)
self.view?.presentScene(nextScene, transition: transition)
}
if nodesArray.first?.name == "easyButton" {
let nextScene = difScene(size: self.size)
self.view?.presentScene(nextScene, transition: transition)
}
if nodesArray.first?.name == "hardButton" {
let nextScene = difScene(size: self.size)
self.view?.presentScene(nextScene, transition: transition)
}
if nodesArray.first?.name == "backButton" {
let nextScene = menuScene(size: self.size)
self.view?.presentScene(nextScene, transition: transition)
}
}
}
}
在情況下,它可能是有用的,這是我GameViewController.swift
:
import UIKit
import SpriteKit
import GameplayKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GKScene(fileNamed: "menuScene") {
// Get the SKScene from the loaded GKScene
if let sceneNode = scene.rootNode as! menuScene? {
// Set the scale mode to scale to fit the window
sceneNode.scaleMode = .aspectFill
// Present the scene
if let view = self.view as! SKView? {
view.presentScene(sceneNode)
view.ignoresSiblingOrder = true
view.showsFPS = false
view.showsNodeCount = false
}
}
}
}
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
}
}
我得到的錯誤是:
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
我知道這是不是一個唯一的錯誤。我有Google,看過教程,並嘗試從其他StackOverflow線程獲得解決方案,但未能克服此問題。
對不起,這是我Java時代的習慣......我有相應的difScene.sks文件,並放置所有按鈕。我到底應該在GameViewController中初始化我的difScene? –
@CevatMertDökümcü你可以在當前的位置初始化它('let nextScene = difScene(size:self.size)'),但是你需要用'fileNamed'而不是'size'調用初始化器。 – nathan
噢好吧我應該怎麼做初始化後調整大小?我曾嘗試使用「nextScene?.size = self.size」,但它不起作用。我目前正在成功進入difScene,但它比預期更大 –