Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I load a spritekit file different than GameScene.swift in my GameViewController.swift?

In GameViewController.swift, I find this line

if let scene = GameScene(fileNamed:"GameScene") {

and I always thought that if I wanted to load something other than GameScene.swift, I could just change "GameScene" to "LandingPage" and everything would work just dandy. However, as I recently figured out, "GameScene" here actually refers to GameScene.sks and not the swift file.

I'm looking to make a game with a number of levels, each written in its own swift file. Where do I go to/ how do I move from, say, level1.swift to level2.swift?

like image 456
Austin Avatar asked Sep 26 '22 15:09

Austin


2 Answers

if you are in one scene for example level selection scene, lets say LevelSelectionScene.swift, you can go to another scene via SKView's -presentScene:transition: method.

class LevelSelectionScene: SKScene {

    override func didMoveToView(view: SKView) { /* ... */}

    func selectLevel(level: Int) {
        let fadeTransition = SKTransition.fadeWithDuration(0.3)
        if let selectedLevel = createLevelSceneWithLevel(level) {
            self.view?.presentScene(selectedLevel, transition: fadeTransition) 
        }
    }


    // Not a good idea if you progressively adding new levels,
    // it's totally depend on how you gonna organize your levels.
    // Since its level input is not arbitrary, the output of this
    // rarely nil, if it does, it must be the developer mistake.
    func createLevelSceneWithLevel(level: Int) -> SKScene? {
        let levelScene: SKScene?
        switch level {
            case 1: levelScene = Level1()
            case 2: levelScene = Level2()
            default: levelScene = nil
        }
        return levelScene
    }

}
like image 164
Kent Liau Avatar answered Oct 24 '22 20:10

Kent Liau


As somebody above has said the line "fileNamed: "GameScene" "

 if let scene = GameScene(fileNamed:"GameScene") {

references the GameScene.sks file, which is the equivalent of storyboard for games.

If you create a new SKScene class and try to load it this way, either from another SKScene or the gameViewController it will not work because it cannot find the corresponding sks file.

I am not 100% sure how to create a new sks file because I dont use it in my games.

If you just want to load a new scene you created you would use this code in your gameViewController

let skView = self.view as! SKView
let scene = NewScene(size: skView.bounds.size)

skView.ignoresSiblingOrder = true
skView.multipleTouchEnabled = true
scene.scaleMode = .AspectFill
skView.presentScene(scene)

If you want to load another scene from a SKScene class its basically the same code

let newScene = NewScene(size: self.size) //size of current scene
let  transition = SKTransition.doorsCloseHorizontalWithDuration(0.5) // use transition between 2 SKScenes
let skView = self.view as! SKView            
skView.ignoresSiblingOrder = true
skView.multipleTouchEnabled = true
newScene.scaleMode = .AspectFill
skView.presentScene(newScene, transition: transition)

or a slightly cleaner version

let newScene = NewScene(size: self.size)
let transition = SKTransition.doorsCloseHorizontalWithDuration(0.5)
newScene.scaleMode = SKSceneScaleMode.AspectFill
self.view?.presentScene(newScene, transition: transition)
like image 43
crashoverride777 Avatar answered Oct 24 '22 21:10

crashoverride777