好吧,所以我在
Swift中有一个sprite kit游戏,我在游戏结束后重启我的GameScene时遇到了麻烦.
现在,当用户失去一生时,变量gameIsOver设置为true,这会暂停场景中的特定节点并启动计时器.在此计时器结束后,我移动到我的Game Over场景.从Game Over场景中,用户可以返回家中或重新开始游戏.
以下是我如何通过场景过渡到我的游戏:
countdown(circle,steps: 120,duration: 5) { //Performed when timer ends self.gameSoundTrack.stop() let mainStoryboard = UIStoryboard(name: "Main",bundle: nil) let vc = mainStoryboard.instantiateViewControllerWithIdentifier("GOViewController") self.viewController!.presentViewController(vc,animated: true,completion: nil) //Resetting GameScene self.removeAllChildren() self.removeAllActions() self.scene?.removeFromParent() self.paused = true gameIsOver = false }
我在这里暂停我的场景是因为,如果我没有,当重新加载GameScene时,gameIsOver仍然设置为true并且我的应用程序崩溃.考虑到我在这里将gameIsOver设置为false,我不知道为什么会这样.
从GameScene转到我的场景游戏并回到GameScene,或者从GameScene转移到我的家庭视图控制器并回到GameScene几次后,我的fps数量下降太多以至于整个游戏都落后于游戏玩法无法实现的程度.
这让我相信,每次我在场景中展示我的游戏时,我都没有正确地移除/处理GameScene.
我相信我遇到了同样的问题:In Swift on “game over” move from scene to another UIView and dispose the scene?,但我是新手,我无法弄清楚他们是如何解决他们的问题的.
我每次呈现Game Over场景时如何完全重置/删除GameScene以阻止滞后?
解决方法
你的gameIsOver Bool在场景之间切换时不会保留一个值,除非你把它变成一个结构.而不是
var gameIsOver = false
它应该是
struct game { static var IsOver : Bool = false }
所以当你改变价值时,你会打电话
game.IsOver = true //if your calling it in a different View controller or scene than where you created it just put the name before it like so GameViewController.game.IsOver = true
func goToGameScene(){ let gameScene:GameScene = GameScene(size: self.view!.bounds.size) // create your new scene let transition = SKTransition.fadeWithDuration(1.0) // create type of transition (you can check in documentation for more transtions) gameScene.scaleMode = SKScenescaleMode.Fill self.view!.presentScene(gameScene,transition: transition) }
然后每当你想重置为GameScene时,只需要打电话
goToGameScene()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。