使用swift在Spritekit中保存已实现的游戏货币

时间:2015-09-18 15:47:58

标签: ios swift

我有一种称为钻石的游戏货币,这是在龙遇到这些钻石时获得的,它们的数量被添加到名为diamondCount的变量中。我正在使用一个名为diamondLabel的标签向用户显示金额,但我已经尝试了很多方法来保存钻石,当你关闭游戏时没有一个对我有效。谁能告诉我一个简单的方法来存储在gamescene.swift中的钻石(我的游戏货币)?

1 个答案:

答案 0 :(得分:1)

我发现以游戏货币保存的最简单方法是,通过将整数值存储到NSUserDefaults,您可以尝试将其链接到Parse等后端服务。这是一个如何准确保存“钻石”的例子。

首先,在GameScene.swift中创建一个名为diamond的全局变量。

import Foundation
import UIKit
import SpriteKit

var diamonds = Int()

class GameScene: SKScene
{
    override func viewMoveToView(view: SKView)
    {
        //Your game code here
    }
}

这将使所有ViewControllers和SKScenes

中的整数变量都可访问

然后你需要在游戏结束时将你的diamondCount存储到NSUserDefaults中。

NSUserDefaults.standardUserDefaults().setInteger(diamondCount, forKey: "totalDiamonds")

让我解释上面的代码行是如何工作的,当你的give结束并且你有一个diamondCount的最终整数值,然后调用那行代码将diamondCount的整数值保存到NSUserDefaults中,以便以后访问数据通过参考键:“totalDiamonds”

现在回答你关于在玩家退出游戏后保存钻石并回来的问题,我已经写了一些代码,你会把它们放到你的AppDelegate.swift中。

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var oldDiamonds : Int = NSUserDefaults.standardUserDefaults().integerForKey("oldDiamonds");

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        diamonds += oldDiamonds

        return true
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.


    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.    
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication)
    {
        NSUserDefaults.standardUserDefaults().setInteger(diamonds, forKey: "oldDiamonds")
        NSUserDefaults.standardUserDefaults().synchronize()
}

希望您了解我对NSUserDefaults的解释。如果您需要更多信息,请查看Apple's Documentation on NSUserDefaults

我希望这会对你有所帮助!评论我需要澄清的任何事情。