Swift: How to handle view controllers for my game

前端 未结 2 1886
太阳男子
太阳男子 2020-12-18 15:43

i have a general question about view controllers and how to handle them in a clean way when i develop a SpriteKit based game.

What i did so far:

  • Use st
相关标签:
2条回答
  • 2020-12-18 16:18

    Go to the segues and use Show Detail Segues anywhere that you don't want the previous view controller to be kept in the stack. Keep in mind that you will have to reinitialize everything appropriately in those view controllers whenever you do return to them.

    If you pay attention, viewDidAppear loads every time that you see the view appear, while with your current setup, viewDidLoad would only be called initially and if you returned to the viewController, only viewDidAppear would be called.

    When you use a segue to transition out of a viewController, prepareForSegue is called, but deinit() is only called when you use a show detail segue (or custom segue with those specific properties about it), because the view, like you said, is loaded into memory so it can be retrieved easier.

    0 讨论(0)
  • 2020-12-18 16:41

    Your way seems very complicated to essentially present 3 scenes. Its not what you are supposed to do for SpriteKit games, you only really need 1 view controller (GameViewController).

    Load your first scene from GameViewController (e.g HomeScene) and nothing else. Create your playButton and other UI directly in HomeScene. Use SpriteKit APIs for your UI (SKLabelNodes, SKNodes, SKSpriteNodes etc).

    You should never really use UIKit (UIButtons, UILabels) in SpriteKit. There are some exceptions to this, like maybe using UICollectionViews for massive level select menus, but basic UI should be done with SpriteKit APIs.

    There is plenty tutorials to google on how to create sprite kit buttons, how to use SKLabelNodes etc. Xcode has a SpriteKit level editor so you can do all that visually similar to storyboards.

    Than from HomeScene transition to the LevelSelect Scene and than to the GameScene and vice versa. Its super easy to do.

    /// Home Scene
    class HomeScene: SKScene {
    
      ...
    
       func loadLevelSelectScene() {
    
           // Way 1
           // code only, no XCode/SpriteKit visual level editor used
           let scene = LevelSelectScene(size: self.size) // same size as current scene 
    
           // Way 2
           // with xCode/SpriteKit visual level editor
           // fileNamed is the LevelSelectScene.sks you need to create that goes with your LevelSelectScene class. 
           guard let scene = LevelSelectScene(fileNamed: "LevelSelectScene") else { return }        
    
    
           let transition = SKTransition.SomeTransitionYouLike
           view?.presentScene(scene, withTransition: transition)
        }  
    }
    
    /// Level Select Scene
    class LevelSelectScene: SKScene {
       ....
    
         func loadGameScene() {
    
            // Way 1
            // code only, no XCode/SpriteKit visual level editor used
            let scene = GameScene(size: self.size) // same size as current scene 
    
            // Way 2
            // with xCode/SpriteKit visual level editor
            // fileNamed is the GameScene.sks you need to create that goes with your GameScene class. 
            guard let scene = GameScene(fileNamed: "GameScene") else { return }
    
    
           let transition = SKTransition.SomeTransitionYouLike
           view?.presentScene(scene, withTransition: transition)
        } 
    }
    
    /// Game Scene
    class GameScene: SKScene {
       ....
    }
    

    I strongly recommend you scratch your storyboard and ViewController approach, and just use different SKScenes and 1 GameViewController.

    Hope this helps

    0 讨论(0)
提交回复
热议问题