Init methods should be used for initialization, but keep in mind that inside init, view is always nil . Thus, any code that needs a view must be ported to the didMoveToView method (it is called immediately after the scene is presented by the view).
About initWithSize in Xcode 6 ... By default, a scene is loaded from a .sks file. Because of this, initWithSize never really called. Instead, initWithCoder is called:
- (instancetype)initWithCoder:(NSCoder *)aDecoder { if (self = [super initWithCoder:aDecoder]) { // do stuff } return self; }
Thus, initializing anything inside initWithSize will have no effect. If you decide to delete the .sks file and create the scene in the βoldβ way, you can do something like this in a view controller:
- (void)viewDidLoad { [super viewDidLoad]; // Configure the view. SKView * skView = (SKView *)self.view; skView.showsFPS = YES; skView.showsNodeCount = YES; /* Sprite Kit applies additional optimizations to improve rendering performance */ skView.ignoresSiblingOrder = YES; // Create and configure the scene. GameScene *scene = [GameScene sceneWithSize:self.view.bounds.size]; scene.scaleMode = SKSceneScaleModeAspectFill; // Present the scene. [skView presentScene:scene]; }
After that, you can use initWithSize to initialize.
Note that in viewDidLoad, the final size of the view is not yet known, and using viewWillLayoutSubviews instead may be the right choice. More details here .
And the correct implementation of viewWillLayoutSubviews for the purpose of initializing the scene:
- (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; // Configure the view. SKView * skView = (SKView *)self.view; skView.showsFPS = YES; skView.showsNodeCount = YES; /* Sprite Kit applies additional optimizations to improve rendering performance */ skView.ignoresSiblingOrder = YES; //viewWillLayoutSubviews can be called multiple times (read about this in docs ) so we have to check if the scene is already created if(!skView.scene){ // Create and configure the scene. GameScene *scene = [GameScene sceneWithSize:self.view.bounds.size]; scene.scaleMode = SKSceneScaleModeAspectFill; // Present the scene. [skView presentScene:scene]; } }
Quick code:
override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene { // Configure the view. let skView = self.view as SKView skView.showsFPS = true skView.showsNodeCount = true skView.showsPhysics = true skView.showsDrawCount = true /* Sprite Kit applies additional optimizations to improve rendering performance */ skView.ignoresSiblingOrder = true /* Set the scale mode to scale to fit the window */ if(skView.scene == nil){ scene.scaleMode = .AspectFill scene.size = skView.bounds.size skView.presentScene(scene) } } }