我正在学习教程
这是我的代码
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
}
override func update(currentTime: NSTimeInterval) {
}
}
如您所见,没有 init 方法。虽然当我在我的 View Controller 中执行此操作时:
//Create and configure the scene
scene = GameScene(size : skView.bounds.size)
场景在哪里:
var scene: GameScene!
它有效。尽管我没有编写代码,但 GameScene 如何批准具有大小的初始化?
如有任何关于 SKSprite 的附加信息,我们将不胜感激。我是这个框架的新手
最佳答案
您的 GameScene
是 SKScene
的子类,因此您从 SKScene
继承了带有大小初始化器的 init。
如果您命令-单击代码中的SKScene
,它将显示该类的标题信息:
public class SKScene : SKEffectNode {
/**
Called once when the scene is created, do your one-time setup here.
A scene is infinitely large, but it has a viewport that is the frame through which you present the content of the scene.
The passed in size defines the size of this viewport that you use to present the scene.
To display different portions of your scene, move the contents relative to the viewport. One way to do that is to create a SKNode to function as a viewport transformation. That node should have all visible conents parented under it.
@param size a size in points that signifies the viewport into the scene that defines your framing of the scene.
*/
public init(size: CGSize)
Xcode 中的另一个有用的技术是 Option - 单击函数调用以找出它们的定义位置。不幸的是,这对初始化器不起作用,除非您在代码中明确调用它们。因此,解决方法是更改:
var scene = GameScene(size : skView.bounds.size)
到
var scene = GameScene.init(size : skView.bounds.size)
然后 Option-点击 init
会出现以下内容:
然后您可以单击SKScene 类引用 了解更多信息。
关于ios - swift2 SKScene 类的对象如何将大小作为参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33447263/