我创建了一款iPhone棋盘游戏。它可以选择用户是想玩多人游戏还是单人游戏。
我已将UIButton
用于棋盘中的棋子。
我有一件事我想解决:在单人游戏模式中,我已经实现了AI UIButton
的{{1}}。但是,在这种情况下,在player1的移动和player2的移动之间没有时间。 两者同时执行。
我想这样做:让人类立即行动,然后计算机会考虑他的举动。我不希望这两个动作走到一起,然后计算机就会想到这一步。
我知道问题是因为游戏在IBAction中读取整个代码块然后他显示结果,但我不知道如何解决这个问题。
有什么建议吗?
如果你需要它,我可以显示代码,但我认为(我也希望),你会理解我的意思。
由于要求它的评论,这里是代码..
- (无效)buttonClickedSinglePlayer:(ID)发送方{
IBAction
[self memorizzastatopartitapassato];
[self volumebottoni];
turno = [self whosTurn];
UIButton *bottoneCampoGioco = sender;
UIButton *bottoneIntelligente;
//Checks if it's Human's turn
if (turno == 0) {
int valoreBottone = [bottoneCampoGioco.currentTitle intValue];
if(valoreBottone< 10){
//if controls are ok, it changes value of human's chosen button
if((bottoneCampoGioco.currentTitleColor != [UIColor greenColor])){
[UIView transitionWithView:bottoneCampoGioco duration:0.3 options:UIViewAnimationOptionTransitionFlipFromLeft animations:nil completion:nil];
[bottoneCampoGioco setTitleColor:[UIColor redColor] forState: UIControlStateNormal];
[bottoneCampoGioco setTitleColor:[UIColor redColor] forState: UIControlStateHighlighted];
bottoneCampoGioco.titleLabel.shadowColor = [UIColor blackColor];
[bottoneCampoGioco.titleLabel setShadowOffset:CGSizeMake(1.0f, 1.0f)]; [bottoneCampoGioco setBackgroundImage: [UIImage imageNamed:@"bottonerosso.png"] forState:UIControlStateNormal];
[self incrementaTitoloOnClick :bottoneCampoGioco];
}
//else alert and loop to first line of the code, until he chooses a valid move
else {
[self posizionenonconsentita];
turno = [self whosTurn];
goto end;
}
}
else{
int x =[self mossedisponibilirosse];
if (x== 1) {
[self analizzavincitore];
}
[self numeroMax];
turno = [self whosTurn];
goto end;
}
turno = 1;
[self analizzaBottoni:bottoneCampoGioco];
[self aggiornapunteggi];
[self memorizzastatopresente];
}
答案 0 :(得分:0)
如果我理解正确,你想在延迟后执行代码的AI部分。如果您可以将代码重构为另一种方法,那么您可以使用performSelector:withObject:afterDelay:
,如下所示:
[self performSelector:@selector(aiMove) withObject:nil afterDelay:2.0];
这将在2秒后运行该方法。或者您可以使用GCD的dispatch_after
,如下所示(下图)。将延迟后要运行的代码放入块中。
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//
//Your code goes here
//
});