Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How we can perform Action on Sequence UIButtons?

enter image description here

As My Screen shot show that i am working on word matching game.In this game i assign my words to different UIButtons in Specific sequence on different loctions(my red arrow shows this sequence)and of rest UIButtons i assign a one of random character(A-Z).when i Click on any UIButtons its title will be assign to UILabel which is in Fornt of Current Section:i campare this UILabel text to below UILabels text which is in fornt of timer.when it match to any of my UILabels its will be deleted.i implement all this process already.

But my problem is that which is show by black lines.if the player find the first word which is "DOG". he click the Two UIButtons in Sequence,but not press the Third one in Sequence.(as show by black line).so here i want that when player press the any UIButtons which is not in Sequence then remove the previous text(which is "DO") of UILabel and now the Text of UILabel is only "G" . Here is my code to get the UIButtons titles and assign it UILabel.

- (void)aMethod:(id)sender 
       {
    UIButton *button = (UIButton *)sender;
    NSString    *get = (NSString *)[[button titleLabel] text];
    NSString *origText = mainlabel.text;
    mainlabel.text = [origText stringByAppendingString:get];

 if ([mainlabel.text length ]== 3) 
    {
if([mainlabel.text isEqualToString: a]){
    lbl.text=@"Right";
    [btn1 removeFromSuperview];
    score=score+10;
    lblscore.text=[NSString stringWithFormat:@"%d",score];
    words=words-1;
    lblwords.text=[NSString stringWithFormat:@"%d",words];
    mainlabel.text=@"";
    a=@"tbbb";
}

    else    if([mainlabel.text isEqualToString: c]){
    lbl.text=@"Right";
    [btn2 removeFromSuperview];
    score=score+10;
    lblscore.text=[NSString stringWithFormat:@"%d",score];
    words=words-1;
    lblwords.text=[NSString stringWithFormat:@"%d",words];
    mainlabel.text=@"";
c=@"yyyy";

}
 else   
     if([mainlabel.text isEqualToString: d]){
    lbl.text=@"Right";
    [btn3 removeFromSuperview];
    score=score+10;
    lblscore.text=[NSString stringWithFormat:@"%d",score];
    words=words-1;
    lblwords.text=[NSString stringWithFormat:@"%d",words];
    mainlabel.text=@"";
    d=@"yyyy";
}
else {
    lbl.text=@"Wrong";
   mainlabel.text=@"";
  }

 }}

Thanx in advance

like image 461
jamil Avatar asked May 29 '12 08:05

jamil


1 Answers

Assign tag to each button from left to right. So you will have,

GButton.tag = 0; TButton.tag = 1; DButton.tag = 2; . . . VButton.tag = 9; . . . EButton.tag = 18; . . . CButton.tag = 26;

Now keep the track of previous pressed button and current pressed button. Call below function when your button delegate hits:

Write below code into your .h file

#define SEQ_TYPE_ANY  0
#define SEQ_TYPE_RIGHT 1
#define SEQ_TYPE_LEFT 2
#define SEQ_TYPE_TOP 3
#define SEQ_TYPE_BOTTOM 4
#define SEQ_TYPE_RIGHT_DIAGONAL_DOWN 5
#define SEQ_TYPE_RIGHT_DIAGONAL_UP 6
#define SEQ_TYPE_LEFT_DIAGONAL_DOWN 7
#define SEQ_TYPE_LEFT_DIAGONAL_UP 8

#define NO_OF_BUTTONS_IN_ROW 9

//Add below variables into your class
int curentSequence;
UILabel *resultLabel;
UIButton *previousButton;

//Declare property for previousButton
@property(nonatomic, retain) UIButton *previousButton;


//Write below code to .m file
@synthesize previousButton;

-(BOOL) isAdjacent:(UIButton *)currentButton
{
     if(previousButton == nil)
     {
          resultLabel.text = currentButton.titleLabel.text;
          curentSequence = SEQ_TYPE_ANY;
          return TRUE;
     }


     if((curentSequence == SEQ_TYPE_ANY || curentSequence == SEQ_TYPE_RIGHT) &&
          (previousButton.tag + 1 == currentButton.tag))
     {
          resultLabel.text = [resultLabel.text stringByAppendingString:currentButton.titleLabel.text];
          curentSequence = SEQ_TYPE_ANY;
          return TRUE;
     }

     else if((curentSequence == SEQ_TYPE_ANY || curentSequence == SEQ_TYPE_LEFT) &&
        (previousButton.tag - 1 == currentButton.tag))
     {
          resultLabel.text = [resultLabel.text stringByAppendingString:currentButton.titleLabel.text];
          curentSequence = SEQ_TYPE_LEFT;
          return TRUE;
     }

     else if((curentSequence == SEQ_TYPE_ANY || curentSequence == SEQ_TYPE_TOP) &&
             (previousButton.tag - NO_OF_BUTTONS_IN_ROW == currentButton.tag))
     {
          resultLabel.text = [resultLabel.text stringByAppendingString:currentButton.titleLabel.text];
          curentSequence = SEQ_TYPE_TOP;
          return TRUE;
     }

     else if((curentSequence == SEQ_TYPE_ANY || curentSequence == SEQ_TYPE_BOTTOM) &&
             (previousButton.tag + NO_OF_BUTTONS_IN_ROW == currentButton.tag))
     {
          resultLabel.text = [resultLabel.text stringByAppendingString:currentButton.titleLabel.text];
          curentSequence = SEQ_TYPE_BOTTOM;
          return TRUE;
     }

     else if((curentSequence == SEQ_TYPE_ANY || curentSequence == SEQ_TYPE_RIGHT_DIAGONAL_DOWN) &&
             (previousButton.tag + NO_OF_BUTTONS_IN_ROW + 1 == currentButton.tag))
     {
          resultLabel.text = [resultLabel.text stringByAppendingString:currentButton.titleLabel.text];
          curentSequence = SEQ_TYPE_RIGHT_DIAGONAL_DOWN;
          return TRUE;
     }

     else if((curentSequence == SEQ_TYPE_ANY || curentSequence == SEQ_TYPE_RIGHT_DIAGONAL_UP) &&
             (previousButton.tag -  NO_OF_BUTTONS_IN_ROW + 1 == currentButton.tag))
     {
          resultLabel.text = [resultLabel.text stringByAppendingString:currentButton.titleLabel.text];
          curentSequence = SEQ_TYPE_RIGHT_DIAGONAL_UP;
          return TRUE;
     }

     else if((curentSequence == SEQ_TYPE_ANY || curentSequence == SEQ_TYPE_LEFT_DIAGONAL_UP) &&
             (previousButton.tag - NO_OF_BUTTONS_IN_ROW - 1 == currentButton.tag))
     {
          resultLabel.text = [resultLabel.text stringByAppendingString:currentButton.titleLabel.text];
          curentSequence = SEQ_TYPE_LEFT_DIAGONAL_UP;
          return TRUE;
     }
     else if((curentSequence == SEQ_TYPE_ANY || curentSequence == SEQ_TYPE_LEFT_DIAGONAL_DOWN) &&
             (previousButton.tag + NO_OF_BUTTONS_IN_ROW - 1 == currentButton.tag))
     {
          resultLabel.text = [resultLabel.text stringByAppendingString:currentButton.titleLabel.text];
          curentSequence = SEQ_TYPE_LEFT_DIAGONAL_DOWN;
          return TRUE;
     }
     else
     {
          resultLabel.text = @"";
          curentSequence = SEQ_TYPE_ANY;
          return FALSE;
     }     
}

// Event handler for button event
- (void)aMethod:(id)sender
{
     UIButton *currentButton = (UIButton *)sender;
     BOOL result = [self isAdjacent:currentButton];
     if(result == FALSE)
     {
          self.previousButton = nil;
          resultLabel.text = @"";
          curentSequence = SEQ_TYPE_ANY;
     }
     else
     {
          self.previousButton = sender;
     }
}

Hope it will help.

like image 159
Apurv Avatar answered Oct 19 '22 15:10

Apurv