How to run different code for different scenes in Xcode sprite-kit?

Viewed 393

I'm building a game in Xcode 8 sprite-kit and I have different levels, but when I load the next level scene, the code for the first level still runs. All of the levels have their own swift files with the same names as their respective scenes. How do I run the code meant for different scenes?

this is the function that changes levels

class func level(_ levelNumber: Int) -> GameScene?
{
    guard let scene = GameScene(fileNamed: "Level_\(levelNumber)") else
    {
        return nil
    }

    scene.scaleMode = .aspectFit
    return scene
}

this is code I call to change levels:

guard let scene = GameScene.level(currentScene) else
{
    print("Level \(self.currentLevel+1) is missing?")
    return
 }

 scene.scaleMode = .aspectFit
 view.presentScene(scene)
1 Answers
Related