0

I created the GameScene first and it works, now I'm trying to add a MainMenu scene which also works except when I hit the button(sprite) to start game, then I get the error above.

The GameViewController is just stock apple code and it starts the app with the MainMenu scene, but I've tested the app just running GameScene in GameViewController and I don't get any error but then i'll have no mainMenu

Within GameScene:

var UpperLeft = SKSpriteNode()
var BottomRight = SKSpriteNode()
var UpperRight = SKSpriteNode()
var BottomLeft = SKSpriteNode()
var Ball = SKSpriteNode()

the error is with each one i'm forcing to unwrap ↓

    override func didMove(to view: SKView) {
    scoreLabel = self.childNode(withName:"scoreLabel") as! SKLabelNode
    UpperLeft = self.childNode(withName:"UpperLeft") as! SKSpriteNode
    BottomRight = self.childNode(withName:"BottomRight") as! SKSpriteNode
    UpperRight = self.childNode(withName:"UpperRight") as! SKSpriteNode
    BottomLeft = self.childNode(withName:"BottomLeft") as! SKSpriteNode
    Ball = self.childNode(withName:"Ball") as! SKSpriteNode

The error is with each one i'm forcing to unwrap ↑

Within MainMenu:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    super.touchesBegan(touches, with: event)

    if let location = touches.first?.location(in: self) {
        let touchedNode = atPoint(location)

        if touchedNode.name == "StartGame" {

            let transition = SKTransition.reveal(with: .down, duration: 1.0)

           let nextScene = GameScene(size: scene!.size)
           nextScene.scaleMode = .aspectFill

            scene?.view?.presentScene(nextScene, transition: transition)
        }
    }
}

Within GameViewController:

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: "mainMenu") {
            // Set the scale mode to scale to fit the window
            scene.scaleMode = .aspectFill

            view.ignoresSiblingOrder = true
            // Present the scene


            view.presentScene(scene)
        }

        view.ignoresSiblingOrder = true

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

I understand that the error comes from the as! forcing the unwrap but I don't see how calling the GameScene from the mainMenu scene causes this to be an issue? Because I did create an object for each unwrap so they should not be nil?

CHiMPS
  • 3
  • 4
  • Please post your code directly in the question in text form. And point out which line caused the error. – Bryan Chen Dec 19 '16 at 03:46
  • 4
    Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – rmaddy Dec 19 '16 at 03:48
  • 1
    `as!` forces the type conversion and fails with a runtime error if the conversion is not possible -- that's the force unwrap error you're seeing. – BallpointBen Dec 19 '16 at 03:48
  • Please refer to: stackoverflow.com/help/how-to-ask. And make the effort to complete the 2-minute site tour as well. – T-Heron Dec 19 '16 at 03:48
  • I already saw that one @rmaddy I understand what it is but I don't understand what causes it, it works when I call the gameScene right from the GameViewController – CHiMPS Dec 19 '16 at 04:34
  • You need to update your question. Post relevant code as actual text copy and pasted into the question (and be sure it is formatted properly). Point out the exact line causing the error. Be sire you provide sufficient details about any relevant variables and any other useful info. – rmaddy Dec 19 '16 at 04:36
  • Ok sorry @rmaddy I am just beginning with this website, I think I have modified the question and provided sufficient details to the question – CHiMPS Dec 19 '16 at 05:06

1 Answers1

2

The problem is in the way you initialize GameScene

use the following code:

 let nextScene = GameScene(fileNamed: "GameScene")

instead of the one used in MainMenu:

 let nextScene = GameScene(size: scene!.size)

when you use size to initialize a scene, it doesn't recognize the .sks file of the scene, so all your nodes which defines in .sks file become nil and that's why you get error while unwrapping them.

Mina
  • 2,167
  • 2
  • 25
  • 32