Swift,spritekit:如何在游戏结束后重启GameScene?停止滞后?

时间:2023-01-23 22:19:24

Alright, so I have a sprite kit game in Swift here and I'm having trouble restarting my GameScene after it's game over.

好吧,所以我在Swift中有一个sprite kit游戏,我在游戏结束后无法重启我的GameScene。

Right now, when the user loses all their lives, a variable gameIsOver is set to true, which pauses specific nodes within the scene as well as sets off a timer. After this timer ends, I move to my Game Over scene. From the Game Over scene, the user can either go back to home or restart the game.

现在,当用户失去一生时,变量gameIsOver设置为true,这会暂停场景中的特定节点并启动计时器。在此计时器结束后,我移动到我的Game Over场景。从Game Over场景中,用户可以返回家中或重新开始游戏。

Here is how I transition to my game over scene:

以下是我如何通过场景过渡到我的游戏:

    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
        }

I pause my scene here is because, if I don't, when GameScene is reloaded gameIsOver is still set to true and my app crashes. I don't know why this occurs considering I set gameIsOver to false here.

我在这里暂停我的场景是因为,如果我没有,当重新加载GameScene时,gameIsOver仍然设置为true并且我的应用程序崩溃。考虑到我在这里将gameIsOver设置为false,我不知道为什么会这样。

After moving from GameScene to my game over scene and back to GameScene, or from GameScene to my home view controller and back to GameScene a couple times, my fps count has decreased so much that the whole game is lagging to the point where gameplay is impossible.

从GameScene转到我的场景游戏并回到GameScene,或从GameScene转移到我的家庭视图控制器并回到GameScene几次后,我的fps数量下降太多以至于整个游戏都落后于无法玩游戏的程度。

This leads me to believe that I am not removing/disposing of GameScene properly each time I present my game over scene.
I believe I'm having the same problem as here: In Swift on "game over" move from scene to another UIView and dispose the scene? , but I'm new to this and I can't figure out how they solved their problem.

这让我相信,每次我在场景中展示我的游戏时,我都没有正确地移除/处理GameScene。我相信我遇到了同样的问题:在Swift上“游戏结束”从场景移动到另一个UIView并处理场景? ,但我是新手,我无法弄清楚他们是如何解决他们的问题的。

How I can I completely reset/delete GameScene each time I present my Game Over scene in order to stop the lagging?

每当我展示Game Over场景以便停止滞后时,我怎样才能完全重置/删除GameScene?

1 个解决方案

#1


5  

Your gameIsOver Bool will not keep a value while switching between scenes unless your make it a struct. So instead of

你的gameIsOver Bool在场景之间切换时不会保留一个值,除非你把它变成一个结构。而不是

var gameIsOver = false

it should be

它应该是

struct game {
    static var IsOver : Bool = false
}

so when you change the value as things happen you call

所以当你改变价值时,你会打电话

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

As for the transition back to your GameScene create a function

至于过渡回你的GameScene创建一个功能

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)
    }

then whenever you want to reset to GameScene just call

然后每当你想重置为GameScene时,只需打电话

goToGameScene()

#1


5  

Your gameIsOver Bool will not keep a value while switching between scenes unless your make it a struct. So instead of

你的gameIsOver Bool在场景之间切换时不会保留一个值,除非你把它变成一个结构。而不是

var gameIsOver = false

it should be

它应该是

struct game {
    static var IsOver : Bool = false
}

so when you change the value as things happen you call

所以当你改变价值时,你会打电话

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

As for the transition back to your GameScene create a function

至于过渡回你的GameScene创建一个功能

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)
    }

then whenever you want to reset to GameScene just call

然后每当你想重置为GameScene时,只需打电话

goToGameScene()