Swift中的viewWillLayoutSubviews

bas*_*nce 6 swift

我正试图转换SKScene * scene = [GameScene sceneWithSize:skView.bounds.size];成swift但我得到了错误

'sceneWithSize'不可用:使用对象构造'SKScene(size :)'.

我正在使用viewWillLayoutSubviews并切割它,viewDidLoad()因为它没有为我选择的设备的屏幕尺寸提供正确的尺寸.它实际上让我怀疑为什么viewDidLoad()存在?

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews();
    let skView = self.view as SKView;
    if skView.scene != nil {
        skView.showsFPS = true;
        skView.showsNodeCount = true;
        skView.showsPhysics = true;
        // Create and configure the scene

        let scene:SKScene = GameScene.sceneWithSize(skView.bounds.size); // ERROR MESSAGE!
        //    Objective-C code in next 2 lines
        //    SKScene * scene = [GameScene sceneWithSize:skView.bounds.size];
        //    scene.scaleMode = SKSceneScaleModeAspectFill;

        // Present Scene
        skView.presentScene(scene)
    }
}
Run Code Online (Sandbox Code Playgroud)

fab*_*789 0

正如错误所示,您尝试使用的功能不可用。您应该使用构造函数init(size size: CGSize)

let scene = SKScene(size: skView.bounds.size)
Run Code Online (Sandbox Code Playgroud)

另请注意,这:SKScene不是必需的,因为类型从构造函数中是显而易见的。

如果您打开documentation,您将看到它+sceneWithSize:不适用于 Swift。