当在 Swift Spritekit 中的 level1Scene 中发生接触时,我如何 运行 我的 GameScene 中的函数?
How do I run a function in my GameScene when a contact occurs in my level1Scene in Swift Spritekit?
所以我在我的 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
来记住您的精灵碰撞,就像在您的 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
方法中,每次当您的视图以这种方式加载时读取:
override func didMoveToView(view: SKView) {
/* Setup your scene here */
let unlockLeavel2 = NSUserDefaults().boolForKey("Leavel2")
if unlockLeavel2 {
unlockLevelTwo()
}
}
即使您退出应用程序,这也会存储。
希望这会有所帮助。
所以我在我的 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
来记住您的精灵碰撞,就像在您的 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
方法中,每次当您的视图以这种方式加载时读取:
override func didMoveToView(view: SKView) {
/* Setup your scene here */
let unlockLeavel2 = NSUserDefaults().boolForKey("Leavel2")
if unlockLeavel2 {
unlockLevelTwo()
}
}
即使您退出应用程序,这也会存储。
希望这会有所帮助。