我试图将我的分数从GameScene传递给GameOverScene
。
我只想在GameOverScene
中设置标签的文本,并使用我传递给它的变量Score。
我正在使用这个:
SKView *spriteView = (SKView *) self.view;
SKScene *currentScene = [spriteView scene];
GameOverScene *gameOverScene = [GameOverScene sceneWithSize:self.frame.size];
[gameOverScene.userData setObject:[currentScene.userData objectForKey:@"score"] forKey:@"score"];
[self.view presentScene:gameOverScene transition:[SKTransition doorsCloseHorizontalWithDuration:1]]; // animation
如何在我的GameOverScene中访问得分变量?
感谢您的回答。
编辑:
好吧我有一个变量int Score; 。如何将objectForKey@"score"
连接到此变量?
EDIT2:
[score setText:[NSString stringWithFormat:@"Score: %@", [self.userData valueForKey:@"score"]]];
我得到分数:(null)
答案 0 :(得分:0)
首先,您需要在userData
中为 GameScene 初始化didMoveToView
:
self.userData = [NSMutableDictionary dictionary];
之后,如果要保存整数score
,请添加:
score = 100; // assume your score is 100 now
[self.userData setObject:[NSString stringWithFormat:@"%d", score] forKey:@"score"];
在呈现下一个场景之前,编辑您在问题中发布的代码,如下所示:
SKView *spriteView = (SKView *)self.view;
SKScene *currentScene = [spriteView scene];
GameOverScene *gameOverScene = [GameOverScene sceneWithSize:self.frame.size];
// Initialize userData for the next scene
gameOverScene.userData = [NSMutableDictionary dictionary];
[gameOverScene.userData setObject:[currentScene.userData objectForKey:@"score"] forKey:@"score"];
NSLog(@"GameScene score: %lu", [[currentScene.userData objectForKey:@"score"] integerValue]);
[self.view presentScene:gameOverScene transition:[SKTransition doorsCloseHorizontalWithDuration:1]];
现在你应该看到你的新场景,并在需要的地方检查分数:
int score = [[self.userData objectForKey:@"score"] intValue];
NSLog(@"GameOverScene score: %d", score);