所以我将类似的游戏编码为4个图像1个单词。我希望当我在第5级时关闭游戏并将其从历史中删除,然后再次返回游戏并按Play我可以在第5级恢复。我尝试了全局变量(LevelNumber)是一个int,其值为1表示它高于classLevel1:Skscene {:
var levelNumber = 1
class Level1: SKScene {
在1级的Win函数中,我将levelNumber的值设为2(因为你完成了1级)并保存:
func Win() {
levelNumber == 2
NSUserDefaults.standardUserDefaults().setInteger(levelNumber, forKey: "levelNumber")
}
*编辑在MainMenu场景中我说:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if levelNumber == 1 {
let Level = Level1(size: self.size)
Level.scaleMode = scaleMode
let transition = SKTransition.fadeWithDuration(0.5)
self.view?.presentScene(Level, transition: transition)
}
else if levelNumber == 2{
let Level = Level2(size: self.size)
Level.scaleMode = scaleMode
let transition = SKTransition.fadeWithDuration(0.5)
self.view?.presentScene(Level, transition: transition)
}
else if levelNumber == 3 {
let Level = Level3(size: self.size)
Level.scaleMode = scaleMode
let transition = SKTransition.fadeWithDuration(0.5)
self.view?.presentScene(Level, transition: transition)
}
所有关卡等等。
继承人AppDelegate.swift中的代码
import UIKit
@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var oldScore : Int = NSUserDefaults.standardUserDefaults().integerForKey("oldScore")
var oldlevelNumber : Int = NSUserDefaults.standardUserDefaults().integerForKey("oldlevelNumber")
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
Score += oldScore
levelNumber += oldlevelNumber
return true
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
NSUserDefaults.standardUserDefaults().setInteger(Score, forKey: "oldScore")
NSUserDefaults.standardUserDefaults().synchronize()
NSUserDefaults.standardUserDefaults().setInteger(levelNumber, forKey: "oldlevelNumber")
NSUserDefaults.standardUserDefaults().synchronize()
}
但出于某些原因,当我按下Play时,转到1级(不调用win功能),然后重新开始游戏并按Play我转到2级甚至没有获胜等级1,为什么?
答案 0 :(得分:2)
在你的Win功能中,设置你的变量:
levelNumber = 2
而不是
levelNumber == 2
答案 1 :(得分:1)
我猜你想要分配score
和levelNumber
而不是添加它们。
你有什么:
score += oldScore
levelNumber += oldlevelNumber
你可能想要什么:
score = oldScore
levelNumber = oldlevelNumber