swift - 当在 Swift Spritekit 的 level1Scene 中发生接触时,如何在我的 GameScene 中运行一个函数?

标签 swift skscene

所以我在我的 GameScene 中有这个功能,它是一个 2 级按钮,它出现在我的 GameScene 中的唯一方式是当我的 level1Scene 中的 HeroCategory 和 GoldKeyCategory 之间发生接触时。当这两个类别之间发生联系时,我将如何调用 levelTwoButton 函数?那有意义吗?我现在有点卡住了,需要帮助。谢谢!

编辑:我希望 2 级按钮显示在我的 GameScene 中,因为那是我的主菜单,而不是 1 级场景。

// GameScene

func unlockLevelTwo() {

let fadeIn = SKAction.fadeInWithDuration(1.0)

levelTwoButton.position = CGPointMake(self.size.width / 2.0, self.size.height / 2.2)
levelTwoButton.zPosition = 20
levelTwoButton.setScale(0.8)
levelTwoButton.alpha = 0
levelTwoButton.runAction(fadeIn)
levelTwoButton.name = "leveltwobutton"
addChild(levelTwoButton)
}



//levelScene

if firstBody.categoryBitMask == HeroCategory && sixthBody.categoryBitMask == GoldKeyCategory{
   GameScene().unlockLevelTwo()
    runAction(playLevelCompleteSound)
    println("you win and level two button unlocked in the main menu!!!!!")
}

最佳答案

您可以使用 NSUserDefaults 来记住您的 Sprite 碰撞,就像在您的 levelScene 中一样,您可以像这样存储它:

if firstBody.categoryBitMask == HeroCategory && sixthBody.categoryBitMask == GoldKeyCategory{
GameScene().unlockLevelTwo()
runAction(playLevelCompleteSound)
println("you win and level two button unlocked in the main menu!!!!!")

NSUserDefaults().setBool(true, forKey: "Leavel2")

}

之后,在您的 GameScene 中,您的 didMoveToView 方法每次在您的 View 以这种方式加载时读取:

 override func didMoveToView(view: SKView) {
    /* Setup your scene here */
    let unlockLeavel2 = NSUserDefaults().boolForKey("Leavel2")

    if unlockLeavel2 {

        unlockLevelTwo()
    }
}

即使您退出应用程序,这也会存储。

希望这会有所帮助。

关于swift - 当在 Swift Spritekit 的 level1Scene 中发生接触时,如何在我的 GameScene 中运行一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30624284/

相关文章:

swift - 如何优雅地使用 guard 或 if swift

ios - 无法以编程方式在我的导航栏中获取 UISearchController

ios - Xcode Scene Editor - 如何显示实体列表?

swift - 我应该使用 SKShapeNode 的替代品来创建绘图吗?如果是这样呢?

ios - Sprite-Kit:留在使用 Tile Editor 创建的边框内

swift - 3D Touch Quick Actions 无法与 SpriteKit 一起正常工作

objective-c - 如何在 Swift 中实现这个多行字符串字面量宏?

swift - 如何将数据发送回之前的接口(interface) Controller ?

swift - contactTestBitMask 用于 nil Physicsbodies

ios - 旋转设备时如何更改 SKScene 大小?