如何使用 swift 在 spriteKit 中使用故事板 [英] How do I use storyboards with spriteKit using swift

查看:28
本文介绍了如何使用 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 的实例上,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 上设计您的 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)
  }
}

关于从场景返回主菜单,看看这个answer.

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

这篇关于如何使用 swift 在 spriteKit 中使用故事板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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