本文介绍了如何使用 swift 使用带有 spriteKit 的情节提要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Xcode 而不是其他应用程序来制作 iOS 应用程序的主要原因之一是故事板界面构建器.当我发现您不打算将情节提要与 spriteKit 一起使用时,我很不高兴.如果没有一个好的视觉构建器,我发现很难为游戏菜单设计一个漂亮的界面.有没有办法使用故事板启动 spriteKit 应用程序,然后单击开始游戏"按钮,切换到 spriteKit 场景,然后当你输掉游戏时,在代码中切换回故事板(使用 swift)?请帮忙,谢谢.

One of my main reason for using Xcode instead of other applications for making iOS apps was the storyboard interface builder. I was unhappy when I found out that you are not meant to use storyboards with spriteKit. I find it hard to design a nice interface for a game's menu without a good visual builder. Is there a way to start a spriteKit application using storyboards, then with a click of a "start game" button, switch to spriteKit scenes, then when you lose the game, in the code switch back to storyboards(using swift)? Please help and thanks.

-卡勒姆-

推荐答案

SpriteKit 场景呈现在 SKView 的实例上,它是 UIView 的子类.

A SpriteKit Scene is presented on an instance of a SKView, which is a subclassed UIView.

对于使用 SpriteKit 制作的 iOS 游戏,需要至少设置一个 viewController,无论是在 App 委托中还是在情节提要中以编程方式设置,SKScene 可以在其上显示.SKScene 将在这个 VC 的主视图中呈现.

For an iOS game made using SpriteKit, one needs to have at least one viewController set up, either programatically in the App delegate or in a storyboard, on which the SKScene can be displayed. It is on the main view of this VC that a SKScene will be presented in.

因此,如果您使用情节提要,iOS 游戏将必须从其中实例化根 viewController.您可以轻松地在 viewController 上设计 UI,并通过按下按钮的代码呈现游戏,无论是在同一个 viewController 中还是在新的 viewController 中.一旦您阅读了使用 Swift 的 SpriteKit 初学者教程(例如 这个.

So if you are using a storyboard, the iOS game will have to instantiate the root viewController from it. You can easily design your UI on the viewController, and present the game from the code on the press of a button, either in the same viewController or a new one. All this will be evident once you read a beginner tutorial for SpriteKit using Swift like this.

假设你的根 viewController 有你的主菜单(在另一个名为 menuView 的视图上),上面有一个播放按钮.现在按一个按钮呈现一个游戏应该是这样的:

Let's say your root viewController has your main menu (on another view called menuView), with a play button on it. Now presenting a game on the press of a button would look something like this:

class MyViewController: UIViewController {
  @IBOutlet wear var menuView: UIView?
  @IBOutlet weak var playButton: UIButton?

  @IBAction func likedThis(sender: UIButton) {
    //Hide the menu view
    menuView.hidden = true

    //instantiate and present the scene on the main view
    let scene = MyScene(size: view.bounds.size)
    let skView = self.view as SKView
    skView.presentScene(scene)
  }
}

至于从场景返回主菜单,看看这个答案.

As for going back to the main menu from the scene, have a look at this answer.

这篇关于如何使用 swift 使用带有 spriteKit 的情节提要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 08:35