Swift:如何为我的游戏处理视图控制器 [英] Swift: How to handle view controllers for my game

查看:90
本文介绍了Swift:如何为我的游戏处理视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于视图控制器的常规问题,以及当我开发基于SpriteKit的游戏时如何以干净的方式处理它们。

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:


  • 仅使用故事板定义视图控制器

  • 每个视图控制器中都会显示SKScene(Home,LevelSelection) ,游戏)by presentScene

  • 在每个视图控制器中我使用在视图控制器之间的故事板中定义的标识符调用 performSegueWithIdentifier

  • 我在故事板上使用SKScene上的SKSpritenode等以编程方式显示的所有内容

  • 我只有视图控制器,其中包含segue关系和标识符

  • 我在viewDidDisappear中所做的所有事情是因为它似乎是让我的SKScene正确消失的唯一方法

  • Use storyboard only for defining view controllers
  • SKScene's are presented in each view controller (Home, LevelSelection, Game) by presentScene
  • in each view controller i call performSegueWithIdentifier with the identifier i defined in the storyboard between the view controllers
  • all the content i show programmatically using SKSpritenode etc. on the SKScene's
  • on the storyboard i only have view controllers with segue relations and identifiers defined
  • all the stuff i do in viewDidDisappear is because it seems to be the only way to get my SKScene deinited correctly

我的问题是:


  • 每当我转向另一个视图时,我的记忆力都会提高,因为视图co ntroller被重新初始化,旧的一直保持在堆栈中

  • 我不清楚如何处理视图控制器之间的segue,在一些教程页面上我看到人们使用导航控制器,其他人使用一些视图控制器的强引用,并使用单一模式的视图控制器,以决定初始化视图控制器或只显示它

  • 我的视图控制器是没有deiniting,我明白我的家庭观点不能,因为它是最初的,但因为ios无论如何重新启动它,为什么然后不卸载它?

使用SpriteKit处理视图控制器的基于Swift的游戏的正确方法是什么?下面你可以看到我的初始视图控制器(Home)显示带有简单播放按钮的SKScene,该按钮调用play()函数以转换到levelselection

What is the correct way for a Swift based game using SpriteKit to handle the view controller? Below you can see my initial view controller (Home) showing an SKScene with a simple play button which calls the play() function to segue to the levelselection

import UIKit
import SpriteKit

class Home : UIViewController {
    private var scene : HomeScene!

    override func viewDidLoad() {
        print(self)
        super.viewDidLoad()
        self.scene = HomeScene(size: view.bounds.size)
        self.scene.scaleMode = .ResizeFill
        let skView = view as! SKView
        skView.showsFPS = true
        skView.showsNodeCount = true
        skView.ignoresSiblingOrder = true

        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(play), name: Constants.Events.Home.play, object: nil)

        skView.presentScene(self.scene)
    }

    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
        let v = view as! SKView
        self.scene.dispose()
        v.presentScene(nil)
        NSNotificationCenter.defaultCenter().removeObserver(self)
        self.scene = nil
        self.view = nil
        print("home did disappear")
    }

    func play() {
        self.performSegueWithIdentifier("home_to_levelselection", sender: nil)
    }

    deinit {
        print("Home_VC deinit")
    }
}


推荐答案

从根本上呈现3个场景看起来非常复杂。它不是你应该为SpriteKit游戏做的,你只需要一个视图控制器(GameViewController)。

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

从GameViewController(例如HomeScene)加载你的第一个场景,别无其他。
直接在HomeScene中创建playButton和其他UI。为您的UI使用SpriteKit API(SKLabelNodes,SKNodes,SKSpriteNodes等)。

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

你永远不应该在SpriteKit中真正使用UIKit(UIButtons,UILabels)。这有一些例外,例如可能使用UICollectionViews进行大规模级别选择菜单,但基本UI应该使用SpriteKit API完成。

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.

有很多关于如何创建精灵工具包按钮,如何使用SKLabelNodes等的教程.Xcode有一个SpriteKit级别编辑器,所以你可以做所有在视觉上类似于故事板。

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.

从HomeScene转换到LevelSelect场景,而不是GameScene,反之亦然。它非常容易。

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 {
   ....
}

我强烈建议你抓住你的故事板和ViewController方法,只需使用不同的SKScenes和1个GameViewController。

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

希望这有助于

这篇关于Swift:如何为我的游戏处理视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆