我在SKScene
中遇到前向声明的问题我必须显示标签节点从游戏场景到场景游戏的值,例如,它返回的值为null
,这是我的代码:
GameOver场景:
#import "MyScene.h"
@class MyScene;
@interface GameOver : SKScene {
MyScene *mainScene;
}
@implementation GameOver
- (void)didMoveToView:(SKView *)view {
scores = [[SKLabelNode alloc]initWithFontNamed:@"Pixel LCD7"];
scores.fontSize = 30;
scores.fontColor = [SKColor darkGrayColor];
//displaying score :
scores.text = [NSString
stringWithFormat:@"Score:%@",mainScene.scoreLabel.text];
scores.name = @"score";
scores.position = CGPointMake(CGRectGetMidX(self.frame), 230);
[self addChild:scores];
}
MyScene
#import "MyScene.h"
@class GameOver;
@interface MyScene : SKScene {
SKLabelNode *scoreLabel;
}
@property (nonatomic) SKLabelNode *scoreLabel;
答案 0 :(得分:1)
它不起作用,因为您以错误的方法初始化标签。 删除didMoveToView:方法,如果您只是用它来初始化和设置标签并将代码移动到initWithSize:方法:
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
// Your init code here
scores = [[SKLabelNode alloc]initWithFontNamed:@"Pixel LCD7"];
scores.fontSize = 30;
scores.fontColor = [SKColor darkGrayColor];
//displaying score :
scores.text = [NSString stringWithFormat:@"Score:%@",mainScene.scoreLabel.text];
scores.name = @"score";
scores.position = CGPointMake(CGRectGetMidX(self.frame), 230);
self addChild:scores];
}
return self;
}
您应该为GameOver场景添加属性以接受您的分数或覆盖initWithSize:例如initWithSize:score:并且您应该在初始化场景游戏时更新您的分数标签。