ios - 在 SpriteKit 中使用另一个场景的 boolean 值

标签 ios swift sprite-kit boolean skscene

我正在尝试在我的 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/

相关文章:

ios - 带有圆角矩形的 SKScene

ios - 使用自定义 SKAction 的 EaseOut Action

swift - 如何通过在屏幕的下半部分沿 y 轴移动手指来旋转彩色 Sprite ?

ios - 子类 UIButton 来保存数据?

ios - 使用屏幕纵横比裁剪部分 UIImage

ios - 如何在 iOS App Bundle 中创建非本地化资源

ios - 我无法删除使用苹果登录的用户

ios - 如何点击 UITableViewCell 中的 UIButton 并转换到新的 View Controller ,并向其传递信息?

ios - 如何创建一个里面有 2 个数组的字典?

ios - 核心数据更新结果值