我怎样才能在其他场景中显示得分?

时间:2014-09-16 01:10:36

标签: objective-c ios7 sprite-kit

我必须在gameOverScene中显示SKLabel的得分。我如何在GameOverScene Label中显示得分?我受试了,请帮助我。

我的游戏场景代码在这里。您可以在下楼梯中查看有关得分的所有详细信息。

MyScene.m

@interface MyScene ()<SKPhysicsContactDelegate>
@property NSUInteger score;
@end

-(void)setupUI
{
    self.score = 0;
    SKLabelNode *scoreLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
    scoreLabel.fontColor = [SKColor redColor];
    scoreLabel.fontSize = 20.0;
    scoreLabel.text = @"SCORE: 0";
    scoreLabel.name = @"scoreLabel";
    scoreLabel.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
    scoreLabel.position = CGPointMake(self.size.width/2, self.size.height - scoreLabel.frame.size.height);
    [self addChild:scoreLabel];
}

-(void)adjustScoreBy:(NSUInteger)points {
    self.score += points;
    SKLabelNode* score = (SKLabelNode*)[self childNodeWithName:@"scoreLabel"];
    score.text = [NSString stringWithFormat:@"SCORE: %lu", (unsigned long)self.score];
}
- (void)gameOver
{
    GameOverScene *gameOverScene = [GameOverScene sceneWithSize:self.size];
    [self.view presentScene:gameOverScene transition:[SKTransition pushWithDirection:SKTransitionDirectionLeft duration:0.5]];
}

GameOverScene.h

@interface GameOverScene : SKScene
@property NSUInteger *score;
@end

GameOverScene.m

@implementation GameOverScene
{
SKLabelNode *scoreLabel;
}

-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
    self.backgroundColor = [SKColor colorWithRed:1.5 green:1.0 blue:0.5 alpha:0.0];
    [self addStartButton];
    [self addRateButton];
    [self addBackButton];
    [self addScoreLabel];
}
return self;
}


-(void)addScoreLabel
{
scoreLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
scoreLabel.text = [NSString stringWithFormat:@"SCORE: %lu", (unsigned long)self.score];
scoreLabel.position = CGPointMake(500, 50);
scoreLabel.name = @"gameOverScore";
[self addChild:scoreLabel];
}

2 个答案:

答案 0 :(得分:2)

有几种方法可以做到这一点。 您可以使用单例类来处理它。 其他选项是在GameOverScene中创建公共分数属性,然后将MyScene的分数值传递给GameOverScene,如下所示:

在GameOverScene.h中添加分数属性

@interface GameOverScene : SKScene
@property NSUInteger score;
@end

然后在你的gameOver方法中设置得分值

- (void)gameOver
{
   GameOverScene *gameOverScene = [GameOverScene sceneWithSize:self.size];
   gameOverScene.score = self.score;
   [self.view presentScene:gameOverScene transition:[SKTransition pushWithDirection:SKTransitionDirectionLeft duration:0.5]];
}   

在GameOverScene中创建didMoveToView

- (void)didMoveToView:(SKView *)view
{
   [self addScoreLabel];
}

答案 1 :(得分:0)

您只需要在下一个场景(目标类)中使用一个属性,然后从(void)gameOver(在源类中)调用它

将它添加到目标类中,就像这样

@property int score;

然后在源类

中使用它
gameOverScene.score = self.score

在移动数据时,你会经常使用这类东西。