0

クイズゲームを作っています。ここに私のコードの一部があります:

- (void) viewDidLoad
{
logos = [[NSMutableArray alloc] init];
[logos addObject:@"adidas"];
[logos addObject:@"nike"];
[logos addObject:@"puma"];

[self showLogo];
}

int currentQuestionIndex;

- (void) showLogo
{

currentQuestionIndex++;

NSString *question = [logos objectAtIndex:currentQuestionIndex];

[_questionImage setImage:[UIImage imageNamed:question]];

...

}

ユーザーが正しい答えを選択すると、次の問題に進むために「showLogo」が再度呼び出されます。ということで、レベル完成です。

すべてがうまく機能します。

情報/レベルを保存するのを手伝ってください。IF レベルが完了すると、ユーザーがゲームを起動するときに、保存されたレベルから開始する必要があります。

これは私がすでに試したことです:

- (void) checkAnswer
{

///... if user answered right

[[NSUserDefaults standardUserDefaults] setInteger:currentQuestionIndex forKey:@"currentLevel"];

[self showLogo];
/// so, I tried to save "currentQuestionIndex" forKey currentLevel, and call "showLogo"
}

ここに変更された「showLogo」があります

- (void) showLogo
{

int savedLevel = [[NSUserDefaults standardUserDefaults] integerForKey:@"currentLevel"];

currentQuestionIndex = savedLevel+1 ;

NSString *question = [logos objectAtIndex:currentQuestionIndex];

[_questionImage setImage:[UIImage imageNamed:question]];

...

}

しかし、機能しません..この方法を使用して、スコアを保存しようとしましたが、それも機能します..

助けてください。ありがとう

4

2 に答える 2

1

ドキュメントに記載されているように、プリミティブ (int) を NSUserDefaults に保存することはできません。次のようにして、int を保存する前に NSNumber に変換する必要があります。

NSNumber *questionIndex = [[NSNumber alloc] numberWithInt:currentQuestionIndex];

次に、それをロードし直すときに、再び int が必要な場合は使用できますintValue

于 2013-03-05T03:03:11.530 に答える
0

にキー値を設定した後NSUserDefaults、メソッドを使用して保存する必要がありますsynchronize

[[NSUserDefaults standardUserDefaults] setInteger:currentQuestionIndex forKey:@"currentLevel"];
[[NSUserDefaults standardUserDefaults] synchronize];
于 2013-03-05T03:02:23.323 に答える