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

时间:2015-10-31 01:48:29

标签: ios iphone swift swift2 skspritenode

我正在学习教程

这是我的代码

class GameScene: SKScene {

    override func didMoveToView(view: SKView) {

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

    }
    override func update(currentTime: NSTimeInterval) {

    }

}

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

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

场景是:

 var scene: GameScene!

它有效。虽然我没有编写代码,GameScene怎么能批准有一个大小的init?

任何有关SKSprite的附加信息都表示赞赏。我是这个框架的新手

1 个答案:

答案 0 :(得分:1)

您的BobGameScene的子类,因此您使用SKScene中的size initializer继承了init。

如果您命令 - 在您的代码中点击SKScene,它将显示该类的标题信息:

SKScene

Xcode中另一个有用的技术是 Option - 点击函数调用以找出它们的定义位置。不幸的是,除非您在代码中明确地调用它们,否则这对初始化器不起作用。因此,解决方法是改变:

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)

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

然后选项 - 点击var scene = GameScene.init(size : skView.bounds.size) ,显示以下内容:

SKScene initializer

然后,您可以点击 SKScene Class Reference 以获取更多信息。