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

标签 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 方法。虽然当我在我的 View Controller 中执行此操作时:

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

场景在哪里:

 var scene: GameScene!

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

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

最佳答案

您的 GameSceneSKScene 的子类,因此您从 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 initializer

然后您可以单击SKScene 类引用 了解更多信息。

关于ios - swift2 SKScene 类的对象如何将大小作为参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33447263/

相关文章:

objective-c - 调试是唯一在 Objective C 程序中使用 iVars 有意义的情况吗?

iOS 位置 - iOS 8/9 中的重大位置更改可以启动标准位置服务吗?

ios - 从导航栏中的栏按钮项到另一个 View Controller 的 Segue 不会运行我的代码?

swift - 用图像计算 UIWebView 高度给出了错误的答案

ios - UIBarButtonItem 未显示在 View 中

ios - 如何在同一个 ViewController 中初始化 NSObject 的两个实例 - Swift

iphone - 如果 NSDate 是星期一

iphone - 在 Objective-C 中无延迟地播放音效

iphone - 我可以用什么来解析 iOS 上的数字证书?

swift - Swift 中 tableView 的分页