SpriteKit中的前向声明

麦克·洛弗

我在前向声明方面遇到问题,例如,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;
格雷格

它不起作用,因为您使用错误的方法初始化了标签。如果仅用于初始化和设置标签,然后将代码移至initWithSize:方法,请删除didMoveToView:方法:

-(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:,并且在初始化游戏场景时应更新分数标签。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章