SpriteKit模板中GameScene.swift和GameScene.sks文件有什么关系 [英] What's the relationship between GameScene.swift and GameScene.sks files in SpriteKit template

查看:21
本文介绍了SpriteKit模板中GameScene.swift和GameScene.sks文件有什么关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩 SpriteKit,并且对如何从代码中驱动它有了相当不错的感觉,但对 Xcode 6 中包含的关卡编辑器感到很困惑.

I've been playing around with SpriteKit, and am getting a pretty decent feel about how to drive it from code, but am pretty baffled by the level editor included in Xcode 6.

我观看了 wwdc 视频(平台联盟状态"和spriteKit 中的新功能"),并在网上四处搜寻,但无法找到关于关卡编辑器及其内容的更多描述真的在做.

I've watched the wwdc videos ("platforms state of union" and "what's new in spriteKit"), and scrounged around the web, but haven't been able to find much description about the level editor, and what it's really doing.

我不明白,模板设置的两个文件有什么关系?.sks 文件是 GameScene.swift 文件的表达式,还是它包含的 GameScene 类?

What I don't understand, is how are the two files that the template sets up related? Is the .sks file an expression of the GameScene.swift file, or perhaps the GameScene class it contains?

或者他们都只是持有将在同一个场景中一起玩的单独的对象/节点?

OR do they both just hold separate objects/nodes that are going to play together in the same scene?

或者(这是我最好的解释).sks 文件和编辑器基本上是用于制作响应式角色将生活"在其中的环境?如果这是真的,我在 .swift 文件中的代码如何与 .sks 文件中的内容相关联?

Or (this is my best explanation) is the .sks file and the editor basically for making the environment that responsive characters are going to "live" in? If that's true, how can my code in the .swift file relate to what's in the .sks file?

推荐答案

.sks 文件是场景内容的静态存档.如果您之前使用过 Interface Builder 来设置 UI 应用程序,那么它的想法大致相同,但在实现上却大不相同.

The .sks file is a static archive of your scene's content. If you've used Interface Builder to set up UI apps before, it's much the same idea, if rather different in implementation.

在 UI 应用中,您可以在代码中完成所有操作:

In a UI app, you could do everything in code:

override func viewDidLoad() {
    let someText = UITextField(...)
    let aButton = UIButton(...)
    // ... position everything
    // ... style everything
    // ... etc ...
}

或者您可以在 IB(xib 或故事板)中设置所有静态内容,并仅使用代码设置应用程序的动态行为——当有人开始触摸这些按钮时发生的事情.当您这样做时,您为其编写代码的视图控制器作为 xib/storyboard 中的代理对象存在,从而在您在 IB 中设置的内容与您在代码中设置的内容之间架起了一座桥梁.

Or you could do all the static content setup in IB (a xib or storyboard), and use code only for setting up the dynamic behavior of your app — the things that happen when somebody starts touching those buttons. When you do that, the view controller you write code for exists as a proxy object in the xib/storyboard, making a bridge between what you set up in IB and what you set up in code.

在 SpriteKit 中,您有同样的选择.在 Xcode 6 之前,很多 SK 游戏都采用了全代码的方式:

In SpriteKit, you have the same choice. Before Xcode 6, many SK games took the all-code approach:

override func didMoveToView(view: SKView) {
    let player = PlumberSprite(color: .Red)
    player.position = // ...
    player.physicsBody = // ...
    self.addChild(player)
    let ground = SKSpriteNode(...)
    ground.position = // ...
    ground.physicsBody = // ...
    self.addChild(ground)
    let block = QuestionBlockSprite()
    block.position = // ...
    block.physicsBody = // ...
    block.contents = CoinSprite()
    self.addChild(block)
    // ... etc etc etc ...
}

对于最终的图形化、静态场景来说,有很多代码——甚至在您开始添加代码以使其进入游戏之前(输入处理、敌人行为、增加分数或结束游戏的物理回调等).而且它不适合将一般游戏逻辑从内容中分离出来的设计,因此更难为游戏添加多个关卡.

That's a lot of code for what's ultimately a graphical, static scene — even before you start adding code to make it into a game (input handling, enemy behavior, physics callbacks that increment the score or go to game over, etc). And it doesn't lend itself well to designs where you split out the general game logic from the content, so it's harder to add multiple levels to your game.

相反,您可以使用 Xcode 中的 SpriteKit 编辑器来构建您的静态内容(关卡),并坚持使用代码来实现动态行为和游戏逻辑.就像在 IB 中,视图控制器是故事板和代码之间的桥梁一样,场景类(模板中的 GameScene.swift)是编辑器和代码之间的桥梁.当您在运行时加载 .sks 文件(此代码在模板中的 GameViewController.swift 中),它会成为您的 GameScene 类,您在编辑器中设置的任何内容都可以作为场景的子节点访问.

Instead, you can use the SpriteKit editor in Xcode to build your static content (levels), and stick to code for dynamic behavior and game logic. Just like how, in IB, the view controller is a bridge between your storyboard and your code, your scene class (GameScene.swift in the template) is the bridge between the editor and code. When you load an .sks file at run time (the code for this is in GameViewController.swift in the template), it becomes an instance of your GameScene class, and anything you set up in the editor is accessible as child nodes of the scene.

查看 WWDC 演讲是个好主意,但您错过了涵盖此内容的演讲:请参阅 session 608:构建 SpriteKit 游戏的最佳实践,了解更多关于 SpriteKit 编辑器背后的动机、如何使用它以及如何处理从场景代码中的 .sks 文件加载的场景内容.

Checking out WWDC talks is a good idea, but you missed the one that covers this: see session 608: Best Practices for Building SpriteKit Games for more on the motivation behind the SpriteKit editor, how to use it, and how to work with the scene contents loaded from an .sks file in your scene code.

这篇关于SpriteKit模板中GameScene.swift和GameScene.sks文件有什么关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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