0

I am making game with 24 levels and I am using NSUserDefaults to let me know what level I am in by setting its value to the level number.

I am trying to get the NSUserDefault value and set it as a string so that when I present the scene it will know what level to go to. But for some reason it is not working. It says I cannot convert a string to an SKScene.

The userdefault saves the number level (1,2,3,etc). With that number I want to go to that scene, which are named (Level1, Level2, etc.) I am trying to attach the number to the string "Level%i" and then present the scene using that string. But it is not letting me it says cannot convert String to SKScene.

Here is the code used to present the level scene:

if self.atPoint(location) == NextLevelButton{

        let LevelSelection = UserDefaults.standard.object(forKey: "LevelSelection") //as? String




        let level2 = NSString(format: "Level%@", LevelSelection! as! SKScene) // Here is where I try to make a string and convert it into a SKScene

        removeAllActions()
        removeAllChildren()
        let scene = level2 (size: self.size) // I get an error here   "Cannot call value of non-function type 'NSString'"
        let sKView = self.view! as SKView
        sKView.ignoresSiblingOrder = true
        scene.scaleMode = .aspectFill

        sKView.presentScene(scene)

Here is the other part of the code in the Level1 scene:

let defaultLevel = 1
UserDefaults.standard.set(defaultLevel, forKey: "LevelSelection")

The Scene's name is Level1, Level2, etc. I just need to append the number of the NSUserDefualt variable to the string "Level" and I have the scene

Does anyone know what am I doing wrong? And if this is not the correct way to do it, What is?

Thank You In Advance !

Alex Merlin
  • 341
  • 1
  • 3
  • 12
  • Are you trying to save an `SKScene` object to `UserDefaults`? If you are, then you can't just convert it to string and save it. – Shamas S Nov 21 '17 at 03:53
  • You can not conver string to skscene, you must save skscene object in userdefault. – Muhammad Shauket Nov 21 '17 at 04:37
  • follow this thread might it will be helpful for you. https://stackoverflow.com/questions/26469457/saving-custom-swift-class-with-nscoding-to-userdefaults @Alex – Muhammad Shauket Nov 21 '17 at 04:45
  • Do you have separate view for every level ? @Alex ? if you have separate views for every level , then just compare your level string to your level numbers that you want to show, after comparing , create skview and present it. – Muhammad Shauket Nov 21 '17 at 04:53
  • I have seperate SKViews for each level ... I am just using the userdefaults to save the number level the user is on ... By knowing the number (1,2,3,etc) I could select the correct SKView (Level1, Level2, etc.) – Alex Merlin Nov 21 '17 at 05:20
  • I just want to know how to make the string ("Level%i") be usable in the code: let scene = Level(1,2,etc) (size: self.size) – Alex Merlin Nov 21 '17 at 05:24
  • I was thinking about that. But then I would have 24 if statements, and I thought there might be a better way – Alex Merlin Nov 21 '17 at 05:29

1 Answers1

1

You can't do that. A string is not convertible to an object.

A possible solution is an array:

let levels : [SKScene] = [level1, level2, level3]

Then you can retrieve the level by index

let levelSelection = UserDefaults.standard.integer(forKey: "LevelSelection")
let scene = levels[levelSelection - 1]
vadian
  • 274,689
  • 30
  • 353
  • 361
  • Hello, when I type the code "let scene = levels[1] (size: self.size)" when changing the scene I get an error saying: "Cannot call value of non-function type 'SKScene' is there a certain way im supposed to write the code ? It has something to do with "(size: self.size)" – Alex Merlin Nov 27 '17 at 03:32
  • Nevermind I fixed the issue. I just had to add the (size: self.size) in with the levels in the array. like this: " let levels : [SKScene] = [level1 (size: self.size), level2 (size: self.size) ] ". Thank You for the help! – Alex Merlin Nov 27 '17 at 05:05