2013-03-18 1 views
0

나는 일련의 깜박이는 단추가 있고, 그 후에 사용자는이 순서를 반복해야한다. 나는 정확한 순서가 눌러 졌는지 탐지하려고한다. 또는 사용자가 누른 순서가 틀리면 (사용자는 동일한 순서로 가야한다) 탐지하려고한다.프레스 순서를 어떻게 추적합니까?

어떻게해야합니까? 나는 모른다. 가능한 한 간단하게 설명하십시오, 나는 이것에 아주 새롭다.

PS kobold2D를 사용 중입니다.

답변

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 
+0

당신은 나에게 예를 들어 줄 수 있을까? 감사! – user2083920

+0

@ user2083920 내 편집 참조 – DrummerB