I updated Xcode to version 6.
Since then I haven't been able to use the code that used to write in Xcode 5 using Objective C.
There are some new files: GameScene.h and GameScene.m and GameScene.sks insteadf of MyScene.h and MyScene.m
They load with:
-(void)didMoveToView:(SKView *)view {
/* Setup your scene here */
}
instead of the usual
-(id) initWithSize: (CGSize)size {
}
I cannot use the code that i used to write in the initWithSize in the didMoveToView, it doesn't work as it should. And if i create a initWithSize myself, it doesn't work either.
Please help!
:)
- didMoveToView: According the the Apple Documentation regarding
SKScene
this method will be called immediately after a scene is presented by a view and the method is intended to be overridden in a subclass.
Discussion
This method is intended to be overridden in a subclass. You can use this method to implement any custom behavior for your scene when it is about to be presented by a view. For example, you might use this method to create the scene’s contents.
Available in 7.0 and later.
- initWithSize: This is called when initializing a new scene object and is only called once.
There isn't much Apple Documentation around initWithSize
but I did find a another question that maybe related to your question.
Where is the right place to configure SKScene content in SpriteKit? - Discusses where is the correct place to configure your SKScene
content and the chosen answer compares the differences and pros of using either initWithSize:
and didMoveToView:
.
Because -(void)didMoveToView:(SKView *)view ;
is a new optional protocol that has to be implemented regardless of whether you use it or not, and so you do not have to have a separate initialization for the Gamescene.m subclass
If you load your scene from SKS file you can't use initWithSize because it never gets called. In that case scene is initialized with initWithCoder.
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
// do stuff
}
return self;
}
About the question initWith... vs didMoveToView.That depends on what you want. Init is for initializing, but you should be aware that self.view is nil while scene's initialization, (read the SO link which @Popeye has provided).