1

I'm trying to setup my scene in my SpriteKit scene file (.sks). I set out my nodes as colored rectangles and using childNodeWithName works fine for deserializing into SKNode objects.

But I'm trying to deserialize into a custom subclass called BadGuyZagNode. The code builds fine but crashes at runtime.

The code:

GameScene.m

-(void)didMoveToView:(SKView *)view {
    BadGuyZagNode *badGuy = (BadGuyZagNode *)[self childNodeWithName:@"badguy"];
    [badGuy setupBadGuyZagNode];
}

BadGuyZagNode.h

@interface BadGuyZagNode : SKSpriteNode
- (void) setupBadGuyZagNode;
@end

BadGuyZagNode.m

@implementation BadGuyZagNode

- (void) setupBadGuyZagNode {
    self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.frame.size];
    self.physicsBody.categoryBitMask = GMCollisionCategoryMinorBadGuy;
    self.physicsBody.collisionBitMask = GMCollisionCategoryLaser;
    self.physicsBody.contactTestBitMask = GMCollisionCategoryLaser;
    self.physicsBody.affectedByGravity = NO;
    self.physicsBody.dynamic = NO;
}

@end

The error

'NSInvalidArgumentException', reason: '-[SKSpriteNode setupBadGuyZagNode]: unrecognized selector sent to instance

Everything works if I don't use childNodeWithName and just init a spriteNodeWithColor.

I've tried adding an init method with no params and calling [super init] but that didn't fly.

I've reviewed both of these answers but neither applied:

Subclassing SKNodes created with SpriteKit .sks scene file

Subclassing SpriteKit classes in Swift

I've also watched the WWCD SpriteKit Best Practices video, but the examples don't use any custom classes.

Community
  • 1
  • 1
gmoore
  • 5,506
  • 5
  • 29
  • 36

1 Answers1

1

There probably isn't enough code posted to give you a more in depth answer. But the short of it is that

[self childNodeWithName:@"badguy"];

doesn't return an instance of a BadGuyZagNode

You can validate this by doing:

BadGuyZagNode *badGuy = (BadGuyZagNode *)[self childNodeWithName:@"badguy"];
NSLog(@"badGuy is %@", NSStringFromClass([badGuy class]));

I didn't fully read Subclassing SKNodes created with SpriteKit .sks scene file, but based on the question/answer, if you want to subclass SKNodes in the SKS, you'll need to do some work (see the answer). If you are not doing this, this is why your node is not a BadGuyZagNode

I'm a little unclear you mean by

Everything works if I don't use childNodeWithName and just init a spriteNodeWithColor.

How are you calling this (i.e. show more code)?

Community
  • 1
  • 1
Mobile Ben
  • 7,121
  • 1
  • 27
  • 43
  • So it sounds like you're saying "It isn't possible" which is a valid answer. I didn't know enough about SpriteKit to know if it was possible. – gmoore Dec 15 '14 at 13:05
  • I'm not saying it's impossible. But if you are using SKS right out of the box with no additional code support, then yes. I'd read http://stackoverflow.com/questions/25123264/subclassing-sknodes-created-with-spritekit-sks-scene-file. Based on that it seems feasible. The way you have your stuff set up right now, it won't work, which is why you are getting the `unrecognized selector sent to instance` error. – Mobile Ben Dec 16 '14 at 01:03