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.