我有一系列闪烁按钮,完成后用户必须重复此顺序。我想检测是否按下了正确的订单,或者检测用户按下的订单是否不正确(用户必须按相同的顺序进行)。
我该怎么做呢?我不知道。请尽可能简单地解释一下,我对此很新。
PS我正在使用kobold2D。
答案 0 :(得分:0)
创建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