Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios SpriteKit中的远期申报_Ios_Objective C_Sprite Kit - Fatal编程技术网

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:中,并且当您在场景上初始化游戏时,应该更新您的分数标签