点滅するボタンのシーケンスがあります。完了後、ユーザーはこの順序を繰り返す必要があります。正しい順序が押されたかどうかを検出したい、またはユーザーが押した順序が間違っているかどうかを検出したい (ユーザーは同じ順序で行かなければならない)。
どうすればこれを行うことができますか?何も思いつきません。私はこれに非常に慣れていないので、できるだけ簡単に説明してください。
PS私はkobold2Dを使用しています。
点滅するボタンのシーケンスがあります。完了後、ユーザーはこの順序を繰り返す必要があります。正しい順序が押されたかどうかを検出したい、またはユーザーが押した順序が間違っているかどうかを検出したい (ユーザーは同じ順序で行かなければならない)。
どうすればこれを行うことができますか?何も思いつきません。私はこれに非常に慣れていないので、できるだけ簡単に説明してください。
PS私はkobold2Dを使用しています。
NSMutableArray
インスタンス変数を作成します。ゲーム/レベルが開始したら、それを空にします。ユーザーがボタンをタップすると、配列に識別子が追加されます(ボタンの番号やタイトル、さらにはボタンオブジェクト自体など)。最後に、この配列を準備された配列と比較するメソッドを実装します(正しいソリューション)。
編集:
これが出発点です。
@interface SomeClassWhereYourButtonsAre
// Array to store the tapped buttons' numbers:
@property (nonatomic) NSMutableArray *tappedButtons;
// Array to store the correct solution:
@property (nonatomic) NSArray *solution;
...
@end
@implementation SomeClassWhereYourButtonsAre
...
- (void)startGame {
self.tappedButtons = [[NSMutableArray alloc] init];
// This will be the correct order for this level:
self.solution = @[@3, @1, @2, @4];
// You probably will have to load this from some text or plist file,
// and not hardcode it.
}
- (void)buttonTapped:(Button *)b {
// Assuming your button has an ivar called number of type int:
[self.tappedButtons addObject:@(b.number)];
BOOL correct = [self correctSoFar];
if (!correct) {
if (self.tappedButtons.count == self.solution.count) {
// success!
} else {
// correct button, but not done yet
} else {
// wrong button, game over.
}
}
- (BOOL)correctSoFar {
// if he tapped more buttons then the solution requires, he failed.
if (self.tappedButtons.count > self.solution.count)
return NO;
for (int i = 0; i < self.tappedButtons; i++) {
if (self.tappedButtons[i] != self.solution[i]) {
return NO;
}
}
return YES;
}
@end