I have a class GameScene, during init of which I make a scene and attach a child to it – a layer. I noticed strange code for it:
CCScene *scene = [CCScene node];
CCLayer *layer = [GameScene node];
[scene addChild:layer];
Why not do this instead?
CCLayer *layer = [CCLayer node];
This doesn’t add to readability or am I missing something fundamental here? Also, why not use alloc init at all?
is a static (class) method. It automatically calls
[[[self alloc] init] autorelease];Any CCNode subclass can be initialized using this static initializer. It is basically for convenience and is really not needed. (Although, like I said, it is nice to have).
Class methods use the “+” sign and instance methods use the “-” sign. Class methods are like static methods in Java. They can be called without creating an instance of that class.
It could look like this:
[YourClass doSomething]; //Doesn't need an instance of YourClassInstance methods need an instance of their class to be able to be run. This would look like:
I haven’t looked, but I assume the
nodemethod looks something like this:The odd declaration just returns a scene (initialized with a static initializer) with your custom layer as a child, that the CCDirector can then use and display.
EDIT: something I feel would add clarity.
The strange declaration is just a static initializer
+ (CCScene*)scenewhich returns a scene.