Ios SpriteKit中的远期申报
我在Ios SpriteKit中的远期申报,ios,objective-c,sprite-kit,Ios,Objective C,Sprite Kit,我在SKScene中有一个转发声明问题,例如,我必须在一个游戏场景到另一个游戏场景中显示标签节点的值,但它返回的值为null,下面是我的代码: 游戏场景: #import "MyScene.h" @class MyScene; @interface GameOver : SKScene { MyScene *mainScene; } @implementation GameOver - (void)didMoveToView:(SKView *)view {
SKScene
中有一个转发声明问题,例如,我必须在一个游戏场景到另一个游戏场景中显示标签节点的值,但它返回的值为null
,下面是我的代码:
游戏场景:
#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;
它不起作用,因为您用错误的方法初始化标签。 Remove-didMoveToView:method,如果您使用它只是初始化和设置标签,并将代码移动到initWithSize:method:
-(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:中,并且当您在场景上初始化游戏时,应该更新您的分数标签