2017-11-25 111 views
1

我試圖用2個按鈕作爲初始場景來呈現菜單:play,credits。當單擊play時,我想用4個按鈕呈現另一個菜單場景:tutorialeasyhard,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線程獲得解決方案,但未能克服此問題。

回答

1

的錯誤是在這條線

tutButton = self.childNode(withName: "tutButton") as! SKSpriteNode // error 

,因爲有一個名爲「tutButton」無子節點,所以迫使它轉換成一SKSpriteNode導致錯誤。

當按下播放按鈕,可以調用此代碼:

let nextScene = difScene(size: self.size) 
self.view?.presentScene(nextScene, transition: transition) 

這初始化一個新difScene,並提出它,進而調用代碼上方時崩潰行。

對於difScene,您是否有對應的sks文件?在didMove中添加您引用的四個按鈕?

如果是這樣,你需要像你這樣爲你的菜單中的場景與sks文件初始化difScene

let scene = GKScene(fileNamed: "difScene") 

如果沒有,您需要創建這四個按鈕節點並將它們添加作爲孩子以編程方式進入你的場景。


旁註:在斯威夫特,慣例是名稱以大寫字母類型。您的menuScene應該是MenuScene,而您的difScene應該是DifScene。這會讓其他人更容易閱讀和理解你的代碼。

+0

對不起,這是我Java時代的習慣......我有相應的difScene.sks文件,並放置所有按鈕。我到底應該在GameViewController中初始化我的difScene? –

+1

@CevatMertDökümcü你可以在當前的位置初始化它('let nextScene = difScene(size:self.size)'),但是你需要用'fileNamed'而不是'size'調用初始化器。 – nathan

+0

噢好吧我應該怎麼做初始化後調整大小?我曾嘗試使用「nextScene?.size = self.size」,但它不起作用。我目前正在成功進入difScene,但它比預期更大 –

相關問題