How to create more SKScene in addition to GameScene

不打扰是莪最后的温柔 提交于 2019-12-10 11:59:35

问题


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)
        }
    }

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


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/34327794/how-to-create-more-skscene-in-addition-to-gamescene

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!