有没有办法统计场景从游戏结束场景转换到游戏场景的次数?
这样做是为了实现激励视频广告。
最佳答案
最简单的方法是使用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/