swift2 SKScene class 的对象如何将大小作为参数?

swift2 how can an object of SKScene class takes a size as a parameter?

我正在学习教程

这是我的代码

class GameScene: SKScene {

    override func didMoveToView(view: SKView) {

    }
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    }
    override func update(currentTime: NSTimeInterval) {

    }

}

如您所见,没有 初始化方法。虽然当我在我的视图控制器中执行此操作时:

//Create and configure the scene
    scene = GameScene(size : skView.bounds.size)

场景是:

 var scene: GameScene!

有效。尽管我没有编写代码,但 GameScene 如何批准具有大小的初始化?

如有任何关于 SKSprite 的附加信息,我们将不胜感激。我是这个框架的新手

您的 GameSceneSKScene 的子 class,因此您从 SKScene.

继承了带有大小初始值设定项的 init

如果您命令-单击您代码中的SKScene,它将显示class的header信息:

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 中的另一个有用技巧是 选项 - 单击函数调用以找出它们的定义位置。不幸的是,这对初始化器不起作用,除非您在代码中明确调用它们。所以解决方法是更改​​:

var scene = GameScene(size : skView.bounds.size)

var scene = GameScene.init(size : skView.bounds.size)

然后 选项 - 单击 init 会出现以下内容:


然后您可以单击 SKScene Class 参考资料 了解更多信息。