我正在尝试在我的 GameViewController.swift 文件上使用我的 GameScene.swift 文件中的变量来适本地安排我的插页式广告的时间。这是一个 boolean 值,用于确定我的玩家是否已死亡。
var died = Bool()
这就是我在 GameScene 中创建变量所做的全部工作。
当在我的 GameScene 中 died == true
时,我想将其发送到我的 GameViewController,然后显示插页式广告。如何在场景之间传递 boolean 值?
最佳答案
您可以按照这些步骤操作。
在您的 GameScene 中执行此操作:
protocol PlayerDeadDelegate {
func didPlayerDeath(player:SKSpriteNode)
}
class GameScene: SKScene {
var playerDeadDelegate:PlayerDeadDelegate?
...
// during your game flow the player dead and you do:
playerDeadDelegate.didPlayerDeath(player)
...
}
在 GameViewController
中,您可以:
class GameViewController: UIViewController,PlayerDeadDelegate {
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene(fileNamed:"GameScene") {
...
scene.playerDeadDelegate = self
}
}
func didPlayerDeath(player:SKSpriteNode) {
print("GameViewController: the player is dead now!!!")
// do whatever you want with the property player..
}
}
关于ios - 在 SpriteKit 中使用另一个场景的 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38176504/