简体   繁体   English

SpriteKit 在两个场景之间转换或移动,使用 GameScene 创建?

[英]SpriteKit Transition or move between two Scenes, creating with GameScene?

I have a SpriteKit app.我有一个 SpriteKit 应用程序。 I would like to simply switch from one view class to another.我想简单地从一个视图 class 切换到另一个视图。 In this case, I would like to just switch from one screen to another screen in the app, so that you can have different things on display that were triggered from a button press or some other logic in the code.在这种情况下,我只想在应用程序中从一个屏幕切换到另一个屏幕,以便您可以显示由按钮按下或代码中的某些其他逻辑触发的不同内容。

I am having trouble creating the second GameScene, as I get some errors.我在创建第二个 GameScene 时遇到了问题,因为我遇到了一些错误。 Could someone provide an example of just switching from one scene to another.有人可以提供一个从一个场景切换到另一个场景的例子。 Here is some code I attempted to get working:这是我试图开始工作的一些代码:


class GameScene: SKScene {
    var player = SKSpriteNode()
    var ground2 = SKSpriteNode()
// The main scene that loads just fine and runs. However I want to transition to another scene, the desert scene. With logic from here to there.
}

class DesertScene : SKScene {
    // inserting my variables, my logic, CGPoints, you get it.
}
func goToScene(newScene: SceneType) {
    var sceneToLoad:SKScene?

    switch newScene {
    case SceneType.GameScene:
        sceneToLoad = GameScene(fileNamed: "GameScene.swift")
    case SceneType.TheWoods:
        sceneToLoad = TheWoods(fileNamed: "TheWoods")
    case SceneType.DesertScene:
        sceneToLoad = DesertScene(fileNamed:"DesertScene")
    }
}

I was not really sure how to transition between either SKS files or even if I should create a new swift file, and have it access GameScene.swift我不确定如何在 SKS 文件之间进行转换,或者是否应该创建一个新的 swift 文件并让它访问 GameScene.swift

If you try to start a new SKScene using the same view controller, the entire previous SKS will be leaked.如果您尝试使用相同的视图 controller 启动新的 SKScene,则整个以前的 SKS 将被泄露。 I've encountered this issue before.我以前遇到过这个问题。

In your case, you want to start a new GameScene.在你的情况下,你想开始一个新的 GameScene。 I'd suggest you creating a new swift file and initialize that gamescene, as you've suggested in your question.正如您在问题中所建议的那样,我建议您创建一个新的 swift 文件并初始化该游戏场景。

Check out my post here, which explains why a new SKScene shouldn't be initialized within the same VC.在这里查看我的帖子,它解释了为什么不应该在同一个 VC 中初始化一个新的 SKScene。

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

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