简体   繁体   English

如何在SpriteKit中重新加载GameScene

[英]How to reload GameScene in SpriteKit

I have a simple game and when the time is up the game is over. 我有一个简单的游戏,时间到了就结束了。 I have a restart button which currently just reloads the GameScene but for some reason my Nodes appear halfway down and halfway to the left of the screen. 我有一个重新启动按钮,该按钮目前仅重新加载GameScene,但由于某种原因,我的节点出现在屏幕左侧和左侧。 I can still use the full space of the screen to play the game though. 我仍然可以使用屏幕的全部空间来玩游戏。

The images below show the game while working, end game state and then after reloading the GameScene. 下图显示了游戏的工作状态,游戏结束状态以及重新加载GameScene之后的状态。

I'm not sure how to fix this or what code to provide. 我不确定如何解决此问题或提供什么代码。

This is my function for reloading the GameScene: 这是我用于重新加载GameScene的功能:

func goToGameScene(){
    let gameScene:GameScene = GameScene(size: self.view!.bounds.size) 
    let transition = SKTransition.fade(withDuration: 1.0)
    gameScene.scaleMode = SKSceneScaleMode.fill
    self.view!.presentScene(gameScene, transition: transition)
}

游戏应该是什么样

结束游戏

重新加载GameScene之后

Replace: 更换:

gameScene.scaleMode = SKSceneScaleMode.fill    

With: 带有:

gameScene.scaleMode = .aspectFit

Or: 要么:

gameScene.scaleMode = .aspectFill  

You can also just completely copy the GameViewController code so you get something like this: 您也可以完全复制GameViewController代码,以便获得如下内容:

if let view = self.view {
            // Load the SKScene from 'GameScene.sks'
            if let scene = SKScene(fileNamed: "GameScene") {
                // Set the scale mode to scale to fit the window
                scene.scaleMode = .aspectFit

                // Present the scene
                view.presentScene(scene)
            }

            view.ignoresSiblingOrder = true

            view.showsFPS = true
            view.showsNodeCount = true
        }

Just put that code where you want to reload the scene. 只需将代码放在要重新加载场景的位置即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM