ios - 计算一下我在 SpriteKit 中失败的游戏次数?

标签 ios swift sprite-kit

有没有办法统计场景从游戏结束场景转换到游戏场景的次数?

这样做是为了实现激励视频广告。

最佳答案

最简单的方法是使用UserDefaults

// GameViewController or MainScene
// when you start the game set the value to 0

UserDefaults.standard.set(0, forKey: "numberOfTries")

// GameScene
// before transitioning to the new scene, increment the value by 1 and save it

var numberOfTries = UserDefaults.standard.integer(forKey: "numberOfTries")
numberOfTries += 1
UserDefaults.standard.set(numberOfTries, forKey: "numberOfTries")

// MenuScene
// get the value and compare it, if it reached the try limit, show video and reset the value

var numberOfTries = UserDefaults.standard.integer(forKey: "numberOfTries")
if numberOfTries == maxNumberOfTriesBeforeVideo {
    // show video
    numberOfTries = 0
    UserDefaults.standard.set(numberOfTries, forKey: "numberOfTries")
}

关于ios - 计算一下我在 SpriteKit 中失败的游戏次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59958548/

相关文章:

ios - 在 iOS 中,如何删除 UINavigationBar 中的 UINavigationBarBackIndicatorView?

ios - 如何在应用程序中使用像素艺术?

swift - 如何在我的 Scenekit.scene 之上创建一个 HUD

ios - UISlider值改变更新结果

ios - UIView 圆角 - Swift 2.0?

ios - Xamarin.Forms WebView、WKWebViewConfiguration 和发送 "Mobile" header

Swift 编译器错误组

ios - Swift:在 touchesBegan/touchesEnded 中延迟双击

ios - 如何将结构或类对象附加到 SCNNode

iOS - 通过共享扩展在应用程序之间移动项目