gpt4 book ai didi

ios - 使用 swift 保存 Spritekit 中已经实现的游戏货币

转载 作者:行者123 更新时间:2023-11-29 01:38:50 25 4
gpt4 key购买 nike

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

最佳答案

我找到了保存游戏货币的最简单方法,前提是您没有尝试将其链接到后端服务,例如 Parse ,是将整数值存储到 NSUserDefaults 中。以下是您如何准确保存“钻石”的示例。

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

import Foundation
import UIKit
import SpriteKit

var diamonds = Int()

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

这将使该整数变量可以在所有 ViewController 和 SKScene 中访问

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

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

让我解释一下上面的代码行是如何工作的,当你的给定结束并且你有一个 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 .

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

关于ios - 使用 swift 保存 Spritekit 中已经实现的游戏货币,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32656132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com