hello,i have a problem with card matching game,this is the button click method and i need a 0.5 second delay in the exact part.i use [NSThread sleepForTimeInterval:0.5] but it works wrong.It should turn both cards and after 0.5 sec if they are not the same it should turn back.And i can't use methods with selectors because they can't take any arguments.
Code:
- (IBAction)ClickedButtons: (id)sender
{
count++;
NSInteger index = [self.cardButtonsindexOfObject:sender];
Card* card = cards[index];
[sender setBackgroundColor:[UIColorwhiteColor]];
[sender setTitle:[NSStringstringWithFormat: @"%@%@",card.rank,card.suit] forState:UIControlStateNormal];
if(count > 1 && lastSender != sender)
{
count = 0;
if([card.rankisEqualToString:lastRank] && [card.suitisEqualToString:lastSuit])
{
UIButton *theButton = (UIButton *)sender;
UIButton *theLastButton = (UIButton *)lastSender;
theButton.enabled = NO;
theLastButton.enabled = NO;
countForWin++;
if(countForWin == 8)
{
UIAlertView *win = [[UIAlertViewalloc] initWithTitle: @"Congratulations"message: @"You Won"delegate:selfcancelButtonTitle: @"Ok"otherButtonTitles: nil];
[win show];
}
}
else
{
[NSThread sleepForTimeInterval:0.5] ;
[sender setBackgroundColor:[UIColorgrayColor]];
[lastSendersetBackgroundColor:[UIColorgrayColor]];
[sender setTitle:[NSStringstringWithFormat: @""] forState:UIControlStateNormal];
[lastSendersetTitle:[NSStringstringWithFormat: @""] forState:UIControlStateNormal];
}
}
elseif(count > 1 && lastSender == sender)
{
flag = !flag;
if(flag)
{
[sender setBackgroundColor:[UIColorgrayColor]];
[sender setTitle:[NSStringstringWithFormat: @""] forState:UIControlStateNormal];
}
else
{
[sender setBackgroundColor:[UIColorwhiteColor]];
[sender setTitle:[NSStringstringWithFormat: @"%@%@",card.rank,card.suit] forState:UIControlStateNormal];
}
}
lastSender = sender;
lastRank = card.rank;
lastSuit = card.suit;
}
Last edited by a moderator: