0
votes

[SKNode node] returns a initlialized node. ( https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKNode_Ref/Reference/Reference.html#//apple_ref/occ/clm/SKNode/node )

If i subclass SKNode:

@interface MyClass : SKNode

And then in my designated initializer inside MyClass i call the super designated initializer (a class method).

@implementation MyClass
{ 
    MyScene _MyScene;
}

- (instancetype)initWithScene:(MyScene *)myScene
{
    _MyScene = myScene;
    if (self = [SKNode node]) { // HERE I GET ERROR
     }
     return self;

}

ERROR: Using the result of an assignment as a condition without parenthesis And if i change it to:

if ((self = [SKNode node]))

I get: incompatible pointer types assigning to (MyClass *) from (SKNode *) What is the way to call super designated initializer if its a class method? I also tried calling self = (MyClass *)[SKNode node] but i get an strange error.

2
By the way: Do not access the _MyScene instance variable (which should really be named lowercase _myScene) before You have assigned the result of [super init] to 'self'. As a memory optimization, init can give you back a different object than you were originally called on. If that happened, you'd not have set the scene on it. - uliwitness

2 Answers

1
votes

I get: incompatible pointer types assigning to (MyClass *) from (SKNode *)

The problem is that [SKNode node] allocates and initializes in instance of SKNode, which means that you don't get the opportunity to turn that object into an instance of your own subclass. You get the incompatible pointer error because you're trying to assign a SKNode * to self, which is of type MyClass *.

Instead, you need to do the allocation yourself, so that enough space is left over for your instance variables after the superclass does its initialization. So, do something like:

self = [super init];
if (self) {
//...
}

I also tried calling self = (MyClass *)[SKNode node] but i get an strange error.

That's basically the same problem. Casting [SKNode node] to type MyClass * doesn't change the fact that the object itself is an instance of SKNode, not an instance of MyClass. Again, you need to make an instance of MyClass (using +alloc) and then call the superclass's (SKNode) initializer, and then do the MyClass initialization.

0
votes

Change this line to:

if (self = [super init]) {
...

You don't want to create a SKNode object but by calling: [SKNode node] you trying to do that.