2012-01-26 64 views
0

我正在開發一款使用遊戲中心的應用程序,但我遇到問題。我希望玩家將他們的分數提交給排行榜,以便他們可以挑戰朋友。這是NSString得分的代碼。將NSString轉換爲Int - 遊戲中心

-(IBAction)gasPedalPressed:(id)sender { 

double noSeconds = (double) [self.startDate timeIntervalSinceNow] * -1000; 


NSString *reactionTime= [[NSString alloc] initWithFormat:@"Good Job! You're reaction time is %1.0f Ms. Let's see if you can do better...", noSeconds]; 

NSString *time= [[NSString alloc] initWithFormat:@"%1.0f Ms", noSeconds]; 


if(greenLightOn == 0) 
    reactionTime = @"Slow down! You have to wait for the green light. Let's see if you can do better..."; 

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Reaction Time" message:reactionTime 
               delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
[alert show]; 

,這就是我想,遵守:

-(IBAction)submitscore:(id)sender { 
GKScore *scoreReporter = [[GKScore alloc] initWithCategory:@"1234567890"]; 

scoreReporter.value = score.text;  
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) { 



    if (error !=nil) {; 
     NSLog(@"failed sub score"); 
    } else { 

     NSLog(@"submitted score"); 

    } 
} 


]; 

}

請幫助!

+0

不是' - [NSString intValue]'爲你工作嗎? – Costique

+0

@Costique我加了這個NSString * myString = [NSString stringWithString:@「time」]; scoreInt = [myString intValue];但它仍然不會工作 – RafeeJ

回答

2

如果你有字符串:

NSSting *myString = [NSString stringWithString:@"2"]; 

你可以從字符串一個int值:

int i = [myString intValue]; 

[編輯] - 在回答您的評論:

由於您已經將noSeconds創建爲double,所以沒有必要將其轉換爲NSString,然後再將其轉換回來。您可以簡單地將noSeconds傳遞給您創建的GKScore實例。

爲了讓您的'submitScore'方法知道您的變量'noSeconds',您將需要將其創建爲實例變量。 (或者你可以只把它傳遞作爲方法的參數)

所以,在你的.h:

double noSeconds; 

@property (nonatomic, assign) double noSeconds; 

然後在您的m:

@synthesize noSeconds; 

-(IBAction)gasPedalPressed:(id)sender { 
    ... 
    noSeconds = (double) [self.startDate timeIntervalSinceNow] * -1000; 
    ... 
} 

-(IBAction)submitscore:(id)sender { 
    ... 
    GKScore *scoreReporter = [[[GKScore alloc] initWithCategory:@"123"] autorelease]; 
    scoreReporter.value = noSeconds; 
    ... 
} 

而且它總是有益採取看一下適用的文檔:GKScore Docs

+0

謝謝,但我將如何實現到我的代碼?抱歉!我是一個完整的初學者! – RafeeJ

+0

@ RafeeSkull-manJenkins - 檢查我更新的答案,希望它有幫助。 – AtkinsonCM

+0

評分依然不會提交!請你能給我發電子郵件,這樣我可以回覆我的項目你能看到我做錯了什麼嗎?非常感謝你! – RafeeJ