所以我在我的 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/