在 SKSpriteNode 中调用自定义方法不起作用
Calling Custom Method Inside SKSpriteNode Not Working
我有一个自定义的 SKSpriteNode 对象,想在其中调用一个自定义方法 "Set Defaults"。
#import <SpriteKit/SpriteKit.h>
@interface Platform : SKSpriteNode
- (instancetype)initWithDynamicPlatform;
- (void)setDefaults;
@end
并且在 .m 文件中
#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
问题是我无法从 SKScene 文件访问自定义方法。
- (void)loadDynamicPlatform {
SKSpriteNode *spritePlatform = [[Platform alloc] initWithDynamicPlatform];
[spritePlatform setDefaults];
[self addChild:spritePlatform];
[self movePlatform:spritePlatform];
}
我收到错误消息
"/Users/****/Desktop/Apps/****/****/GameScene.m:142:21:
No visible @interface for 'SKSpriteNode' declares the selector
'setDefaults'"
知道为什么我无法访问它。我确信我设置正确。
您必须声明一个 'Platform' 变量或将 'SKSpriteNode' 转换为 'Platform':
Platform *spritePlatform = [[Platform alloc] initWithDynamicPlatform];
[spritePlatform setDefaults];
我有一个自定义的 SKSpriteNode 对象,想在其中调用一个自定义方法 "Set Defaults"。
#import <SpriteKit/SpriteKit.h>
@interface Platform : SKSpriteNode
- (instancetype)initWithDynamicPlatform;
- (void)setDefaults;
@end
并且在 .m 文件中
#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
问题是我无法从 SKScene 文件访问自定义方法。
- (void)loadDynamicPlatform {
SKSpriteNode *spritePlatform = [[Platform alloc] initWithDynamicPlatform];
[spritePlatform setDefaults];
[self addChild:spritePlatform];
[self movePlatform:spritePlatform];
}
我收到错误消息
"/Users/****/Desktop/Apps/****/****/GameScene.m:142:21: No visible @interface for 'SKSpriteNode' declares the selector 'setDefaults'"
知道为什么我无法访问它。我确信我设置正确。
您必须声明一个 'Platform' 变量或将 'SKSpriteNode' 转换为 'Platform':
Platform *spritePlatform = [[Platform alloc] initWithDynamicPlatform];
[spritePlatform setDefaults];