分数从skscene转移到otherscene

时间:2021-05-20 23:07:32

I can't receive score in other scene. I setup @propery but it doesn't works. Score is "0" every time it not change. My codes here.

我无法在其他场景中获得分数。我设置@propery但它不起作用。每次不改变时得分为“0”。我的代码在这里。

OtherScene.h

OtherScene.h

@interface OtherScene : SKScene
@propery NSUInteger score;
@end

OtherScene.m

OtherScene.m

@implementation OtherScene
{
SKLabelNode *scoreLabel;
}

-(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];
}

MyScene.m

MyScene.m

@interface MyScene ()<SKPhysicsContactDelegate>
@property NSUInteger score;
@end
- (void)gameOver
{

GameOverScene *gameOverScene = [GameOverScene sceneWithSize:self.size];
gameOverScene.score = self.score;
[self.view presentScene:gameOverScene transition:[SKTransition pushWithDirection:SKTransitionDirectionLeft duration:0.5]];
}

1 个解决方案

#1


0  

When/where do you call addScoreLabel?

何时/何地调用addScoreLabel?

If this is in OtherScene init you're effectively running it in the line

如果这是在OtherScene init中你有效地在行中运行它

GameOverScene *gameOverScene = [GameOverScene sceneWithSize:self.size]; 

Meaning: before you assigned the score. You could simply call the update label method from MyScene after assigning the score:

含义:在分配分数之前。您可以在分配分数后从MyScene调用更新标签方法:

GameOverScene *gameOverScene = [GameOverScene sceneWithSize:self.size];
gameOverScene.score = self.score;
[gameOverScene addScoreLabel];

Or even better, do this in a custom property setter method in OtherScene, ie setScore:(NSUInteger)score and separate creating the label from updating its score string to be more flexible.

或者甚至更好,在OtherScene中的自定义属性设置器方法中执行此操作,即setScore:(NSUInteger)得分并单独创建标签以更新其得分字符串以更灵活。

#1


0  

When/where do you call addScoreLabel?

何时/何地调用addScoreLabel?

If this is in OtherScene init you're effectively running it in the line

如果这是在OtherScene init中你有效地在行中运行它

GameOverScene *gameOverScene = [GameOverScene sceneWithSize:self.size]; 

Meaning: before you assigned the score. You could simply call the update label method from MyScene after assigning the score:

含义:在分配分数之前。您可以在分配分数后从MyScene调用更新标签方法:

GameOverScene *gameOverScene = [GameOverScene sceneWithSize:self.size];
gameOverScene.score = self.score;
[gameOverScene addScoreLabel];

Or even better, do this in a custom property setter method in OtherScene, ie setScore:(NSUInteger)score and separate creating the label from updating its score string to be more flexible.

或者甚至更好,在OtherScene中的自定义属性设置器方法中执行此操作,即setScore:(NSUInteger)得分并单独创建标签以更新其得分字符串以更灵活。