2015-09-16 124 views
0

我試圖將我的分數從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分數; 。我如何將[email protected]"score"連接到這個變量?

EDIT2:

[score setText:[NSString stringWithFormat:@"Score: %@", [self.userData valueForKey:@"score"]]]; 

有了,我得到的分數:(null)

+0

http://stackoverflow.com/questions/18885490/spritekit-passing-data-between-scenes –

回答

1

首先,你需要在didMoveToView初始化userDataGameScene

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); 
+0

但是,我如何給我的可變分數我的關鍵分數?它沒有按照我的方式工作 – Robin

+1

@Robin你已經在** GameScene **中爲'userData'設置了整數'score'嗎? – WangYudong

+0

不,我該怎麼做? – Robin