2016-04-29 53 views
0

我有兩個場景 - 在GameScene我得到勝利計數並將其保存到BonusScene,每件事情都很好,但我想每次顯示GameScene贏得計數,我在BonusScene重新啓動應用程序它顯示右算,但在GameScene它1.從這裏開始就是我一直在使用:GameScene.m保存,加載和更新贏得計數使用NSUserDefaults在目標c

-(void)winCount{ 
win++; 
winningCount =winningCount +win; 
NSLog(@"%i",win); 
NSUserDefaults *scoreprefs = [NSUserDefaults standardUserDefaults]; 
[scoreprefs setInteger:winningCount forKey:@"winningCount"]; 

winLabel = [SKLabelNode labelNodeWithFontNamed:@"ROTORcapExtendedBold"]; 
winLabel .fontSize = 70.f; 
winLabel .position = CGPointMake(CGRectGetMidX(self.frame)+(CGRectGetMidX(self.frame)/2),CGRectGetMidY(self.frame)); 
winLabel .zPosition = 5; 
winLabel .text = [NSString stringWithFormat:@"%ld",(long)winningCount]; 
[self addChild:winLabel]; 

而且在BonusScene.m

@implementation BonusScene 
@synthesize winningCount; 
-(id)initWithSize:(CGSize)size{ 
NSUserDefaults *scoreprefs = [NSUserDefaults standardUserDefaults]; 
self = [super initWithSize:size]; 
if (self){ 
winningCount =[scoreprefs integerForKey:@"winningCount"]; 
    SKLabelNode *wins = [SKLabelNode labelNodeWithFontNamed:@"ROTORcap Extended Bold"]; 
    wins.text =[NSString stringWithFormat:@"%ld",(long)winningCount ]; 
    wins.position = CGPointMake(CGRectGetMidX(self.frame) + 100,(CGRectGetMidY(self.frame)) + self.frame.size.height/4); 
    wins.fontSize = 15.f; 
    [self addChild:wins] 

如何在GameScene中更新它,以便顯示與BonusScene相同的結果?

回答

1

更新您的遊戲場景與

-(void)winCount{ 
    // get previous count first 
    winningCount =[scoreprefs integerForKey:@"winningCount"]; 
    win++; 
    winningCount = winningCount + win; 
    NSLog(@"%i",win); 
    // other code of the function 
+0

是的,現在的工作,謝謝! – artG