这里我有一个非常简单的SKLabelNode子类实现。
界面:
#import <SpriteKit/SpriteKit.h>
@interface CSLabelNode : SKLabelNode
- (instancetype)initWithFontNamed:(NSString *)fontName fontSize:(CGFloat)fontSize fontColor:(SKColor *)fontColor;
@end
实施:
#import "CSLabelNode.h"
@implementation CSLabelNode
- (instancetype)initWithFontNamed:(NSString *)fontName fontSize:(CGFloat)fontSize fontColor:(SKColor *)fontColor
{
self = [super initWithFontNamed:fontName];
if (self) {
self.fontSize = fontSize;
self.fontColor = fontColor;
}
return self;
}
@end
我正在调用常规的SKLabelNode和我的自定义CSLabelNode:
CSLabelNode *custom = [[CSLabelNode alloc] initWithFontNamed:@"CrimsonText-Bold" fontSize:30.0f fontColor:[SKColor redColor];
SKLabelNode *regular = [[SKLabelNode alloc] initWithFontNamed:@"CrimsonText-Bold"];
custom.text = @"Some text";
regular.text = @"Some text";
问题是我的自定义节点的框架是 origin =(x = 0,y = 0)size =(width = 0,height = 0),而常规节点的框架是,与预期一样: origin =(x = -43,y = -1)size =(width = 86,height = 25)
如何让自定义对象拥有非零帧属性?