1
votes

I tried to create a new subclass of SKScene "MainScene" like the one apple created the GameScene.

I want to create more scene in addition to my "GameScene" but its not working.

Below is my subclass code.

MainScene :

import SpriteKit
#if !os(iOS)
import AppKit
#endif

class MainScene : SKScene {

    override func didMoveToView(view: SKView) {

        backgroundColor = UIColor.blueColor()

       }

}

MainSceneViewController :

import UIKit
import SpriteKit

class MainViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if let scene = MainScene(fileNamed:"MainScene") {
            // Configure the view.
            let skView = self.view as! SKView
            //skView.showsFPS = true
            //skView.showsNodeCount = true

            /* Sprite Kit applies additional optimizations to improve rendering performance */
            skView.ignoresSiblingOrder = true
            skView.showsPhysics = true

            /* Set the scale mode to scale to fit the window */
            scene.scaleMode = .AspectFill

            skView.presentScene(scene)
        }
    }

enter image description here Error : "Could not cast value of type 'UIView' (0x1097f2b20) to 'SKView' (0x108a4cad0)."

2
Perhaps it is where you have named your class 'MainView' instead of 'MainScene' where you have declared it, in your UIViewController? - mdhomer
A lot of people always seem to miss this part, so I am going to ask, did you make a MainScene.sks file - Knight0fDragon
Then that is your problem MainScene(fileNamed:"MainScene") is looking for MainScene.sks, not MainScene.swift. MainScene.sks is your actual scene file, it holds all of the preliminary data for a scene. MainScene.swift is just the code behind the scene to tell it how it should act - Knight0fDragon

2 Answers

4
votes

When creating a new scene, you need to actually create a new scene file, not just the code behind file.

To create new scene files, go to file, New, File, then in your sections go to iOS, SpriteKit Scene. This will create the sks file you need.

edit:

In the case you are getting the error Could not cast value of type 'UIView' to 'SKView', this is due to the fact that in your story board, your ViewControllers main view does not have a custom class of SKView. In your storyboard file, open up your view controller, click the view for this controller, make sure the right panel is visible, and select the identity inspector tab (It is probably the 3rd from left, looks like a flag or an envelope with a window). Under Custom Class, look for the class text box, and put in SKView

4
votes

To create more SKScene in addition to GameScene

  • Go to New File
  • Select Coca Touch Class
  • Click on Next
  • SubClass : Type Manually SkScene
  • create
  • import SpriteKit