ViewWillLayoutSubviews in Swift

I try to translate SKScene * scene = [GameScene sceneWithSize:skView.bounds.size]; in swift, but getting an error

'sceneWithSize' is not available: use the object construct 'SKScene (size :)'.

I use viewWillLayoutSubviews and viewDidLoad() because it does not give the correct sizes for the screen sizes of the device that I select. This really makes me wonder why viewDidLoad() exists at all?

 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) } } 
+6
source share
3 answers

Try to change

 let scene:SKScene = GameScene.sceneWithSize(skView.bounds.size); 

by

 let scene:SKScene = GameScene(size: skView.bounds.size); 

Hope this helps;)

+4
source

As the error says, the function you are trying to use is not available. Instead, you should use the init(size size: CGSize) constructor init(size size: CGSize) :

 let scene = SKScene(size: skView.bounds.size) 

Note also that :SKScene not required because the type is obvious from the constructor.

If you open the documentation , you will see that +sceneWithSize: not available for Swift.

0
source

When a view change is limited, the view adjusts the position of its subzones. The controller of your view can override this method to make changes before the view displays its subitems. By default, the implementation of this method does nothing.

0
source

Source: https://habr.com/ru/post/979096/


All Articles