나는 24 레벨로 게임을하고 있으며 NSUserDefaults를 사용하여 레벨 값으로 값을 설정하여 내가 어떤 레벨인지 알 수 있습니다.SKScene의 이름으로 문자열을 사용하는 방법
NSUserDefault 값을 가져 와서 문자열로 설정하려고하므로 장면을 제시 할 때 어떤 레벨로 갈지 알 수 있습니다. 그러나 어떤 이유로 그것은 작동하지 않습니다. 문자열을 SKScene으로 변환 할 수 없다고합니다.
userdefault는 숫자 수준 (1,2,3 등)을 저장합니다. 그 번호로 나는 그 장면으로 가고 싶다. (Level1, Level2 등) 나는 "Level % i"문자열에 숫자를 붙이고 그 문자열을 사용하여 장면을 제시하려고한다. 하지만 그것은 String을 SKScene으로 변환 할 수 없다는 것을 알려주지 않습니다. ,
let defaultLevel = 1
UserDefaults.standard.set(defaultLevel, forKey: "LevelSelection")
장면의 이름은 레벨 1, 레벨 2입니다 :
여기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)
는 레벨 1의 장면에서 코드의 다른 부분입니다 : 여기
는 수준의 장면을 제시하는 데 사용되는 코드입니다 NSUserDefualt 변수의 번호를 문자열 "Level"에 추가하면됩니다.내가 뭘 잘못하고 있는지 알 수 있습니까? 그리고 이것이 올바른 방법이 아니라면 무엇입니까?
미리 감사드립니다!
당신이 UserDefaults''에'SKScene' 객체를 저장하려고 지수로 수준을 검색 할 수 있습니다
이 가능한 솔루션은 배열입니다? 그렇다면 문자열로 변환하고 저장할 수 없습니다. –
문자열을 skscene로 변환 할 수 없으면 userdefault에 skscene 객체를 저장해야합니다. –
이 스레드를 따라하면 도움이 될 수 있습니다. https://stackoverflow.com/questions/26469457/saving-custom-swift-class-with-nscoding-to-userdefaults @Alex –