6

I'm trying to translate SKScene * scene = [GameScene sceneWithSize:skView.bounds.size]; into swift but I get the error

'sceneWithSize' is unavailable: use object construction 'SKScene(size:)'.

I am using viewWillLayoutSubviews and cutting out the viewDidLoad() because it does not give correct dimensions for the screen dimensions of the device I choose. It actually makes me question 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)
    }
}
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
baskInEminence
  • 710
  • 11
  • 26

3 Answers3

4

Try changing

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

by

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

Hope this helps ;)

smorollon
  • 56
  • 1
  • 3
0

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

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

Note also that the :SKScene is not necessary as the type is obvious from the constructor.

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

fabian789
  • 8,348
  • 4
  • 45
  • 91
  • I commented out all the lines within viewDidLoad except for super.viewDidLoad() and put in viewWillLayoutSubviews() with your suggestions but now I see nothing in the simulator. In Objective C, I always specified GameScene sceneWithSize but I'm assuming the compiler doesn't know since I said let scene = SKScene(size: skView.bounds.size) and there is no indication of GameScene. Just an assumption. Any ideas on how I can fix this? – baskInEminence Dec 04 '14 at 18:39
0

When a view's bounds change, the view adjusts the position of its subviews. Your view controller can override this method to make changes before the view lays out its subviews. The default implementation of this method does nothing.

BhuShan PaWar
  • 79
  • 1
  • 6