4

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! :)

Zigii Wong
  • 7,766
  • 8
  • 51
  • 79
Rohan Kalantri
  • 103
  • 1
  • 9

3 Answers3

1
  • 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:.

Community
  • 1
  • 1
Popeye
  • 11,839
  • 9
  • 58
  • 91
0

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

Ranhiru Jude Cooray
  • 19,542
  • 20
  • 83
  • 128
  • 1
    Where does it say that `didMoveToView:` must be implemented? And where does it say that `didMoveToView:` is an optional protocol? If you look within the Apple Documentation this isn't stated anywhere and if you checkout `SKScene.h` this is just a method declared the normal way you'd declare a method and it isn't declare as a protocol. Based on this, I'd say this answer is incorrect. Also if you check the documentation it states this method is intended to be overridden in the subclass so that last statement in your answer is definitely incorrect. – Popeye Mar 18 '15 at 08:46
0

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).

Whirlwind
  • 14,286
  • 11
  • 68
  • 157