Reputation: 3233
I have a custom SKSpriteNode object and want to call a custom method inside it "Set Defaults".
#import <SpriteKit/SpriteKit.h>
@interface Platform : SKSpriteNode
- (instancetype)initWithDynamicPlatform;
- (void)setDefaults;
@end
And in the .m file
#import "Platform.h"
@implementation Platform
- (instancetype)initWithImageNamed:(NSString *)name {
if (self == [super initWithImageNamed:name]) {
NSLog(@"Initiated Platform");
}
return self;
}
- (instancetype)initWithDynamicPlatform {
if (self == [super initWithImageNamed:@"Platform2"]) {
NSLog(@"Initiated Platform");
}
[self setDefaults];
return self;
}
- (void)setDefaults {
/**
* Set the name
*/
self.name = @"Platform";
/**
* Set the effect of gravity on the platform
*/
self.physicsBody.affectedByGravity = NO;
self.physicsBody.dynamic = NO;
}
@end
The problem is that from the SKScene file I cannot access the custom method.
- (void)loadDynamicPlatform {
SKSpriteNode *spritePlatform = [[Platform alloc] initWithDynamicPlatform];
[spritePlatform setDefaults];
[self addChild:spritePlatform];
[self movePlatform:spritePlatform];
}
I get the error message
"/Users/****/Desktop/Apps/****/****/GameScene.m:142:21: No visible @interface for 'SKSpriteNode' declares the selector 'setDefaults'"
Any idea why I can not access this. I am sure I was setting it up correctly.
Upvotes: 2
Views: 53
Reputation: 5451
You have to declare a 'Platform' variable or cast the 'SKSpriteNode' to 'Platform':
Platform *spritePlatform = [[Platform alloc] initWithDynamicPlatform];
[spritePlatform setDefaults];
Upvotes: 4