1
votes

I'm trying to figure out how to add more scenes in SpriteKit. If I use the line generated from SpriteKit in the GameViewController

if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene 

This works fine, however if I create a class that's also a of type SKScene and add it the the SKView.

let newscene = GameMenu()
skView.presentScene(newscene)

This will not work. It just displays a washed out gray color with only a fraction of the nodes but nothing actually presented. Is there something going wrong with the way SpriteKit uses unarchiveFromFile to create the scene. I'm also not even using the Storyboard or GameScene.sks. The entire game has been created programatically. One more Issue if I try to create another ViewController for a menu and use presentViewController. When it tries to run I also get an error that says "Could not cast value of type 'UIView' to 'SKView.'" I'm casting it with the same line similar to GameViewController.

let skView = self.view as! SKView

This is the line that it keeps breaking on. If anyone has any insight on how to fix this problem that would be greatly appreciated.

2
What does the init method of GameMenu look like? Edit your question to include that code. - rob mayoff

2 Answers

4
votes

Here is how I normally make a scene:

  1. Go to File -> New -> File...
  2. Click Cocoa Class, click next
  3. Name it whatever you want, make it a subclass of SKScene, choose swift
  4. In the new class, make it look like this:

    import SpriteKit
    
    class GameScene: SKScene {
    
        override init(size: CGSize) {
            super.init(size: size)
    
            //Other init code
        } 
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
        }
    
        //Other functions like override func touchesBegan, update, etc. Whatever you want
    }
    
  5. If you want to display the scene first, GameViewController, in viewDidLoad you would type:

    let scene = GameScene(size: skView.bounds.size)
    skView.presentScene(scene) 
    

    (skView should be made for you already, but if its not , its: let skView = self.view as! SKView, and put that before all the let scene = GameScene... etc and stuff in viewDidLoad.

  6. Then if you wanted to present the scene from a different SKScene, and not from the view controller, then do:

    let scene = GameScene(size: self.frame.size)
    self.view?.presentScene(scene)
    
1
votes

If you're constructing a scene programmatically, don't try to unit hive it from a file (that probably stores a different scene). Use its initializer instead: e.g. GameMenu() or GameMenu(size: whateverSize) or a custom initializer you've defined.