我制作了一个iPhone棋盘游戏。它有能力选择是用户想玩多人还是单玩家。
我用UIButton
来做棋盘上的零件。
我有一件事要修复:在单玩家模式下,我实现了AI的转换为UIButton
的IBAction
。但是,在这种情况下,玩家1的移动和玩家2的移动之间没有时间。同时被执行。。
,我想这样做:让人类立即移动,然后计算机为他的移动而思考。我不想让这两个动作结合在一起,在那台电脑考虑移动之后。
我知道问题在于,因为游戏读取了IBAction中的整个代码块,然后他显示了结果,但我不知道如何解决这个问题。
有什么建议吗?
如果您需要它,我可以展示代码,但我想(我也希望如此),您将理解我的意思。
由于请求的评论,这是代码..。
-(ButtonClickedSinglePlayer):(Id)发件人{
[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];
pragma标记单个玩家的动作
//Azione del giocatore singolo
//changes turn token's value to allow to AI to play
turno = [self whosTurn];
turno = 0;
ai = [[ArtificialIntelligence alloc]init];
//send to AI's class the human's pressed button
[ai setbottonepremuto:bottoneCampoGioco];
[ai setDimensioneInput:dimensioneInput];
//send the situation of the board
[ai setSituazione:arraycampogioco];
[ai setpunteggiogiocatori:volumerossi giocatoree2:volumeverdi];
//get back the chosen button from ai's class
bottoneIntelligente=[ai bottone];
[mossaEffettuata setText:[self stringaCoordinateDaBottone:bottoneIntelligente]];
int valoreBottone2 = [bottoneIntelligente.currentTitle intValue];
//then changes the value of the button chosen from ai's class
if(valoreBottone2< 10){
[UIView transitionWithView:bottoneIntelligente duration:0.3 options:UIViewAnimationOptionTransitionFlipFromTop animations:nil completion:nil];
[bottoneIntelligente setTitleColor:[UIColor greenColor] forState: UIControlStateNormal];
[bottoneIntelligente setTitleColor:[UIColor greenColor] forState: UIControlStateHighlighted];
bottoneIntelligente.titleLabel.shadowColor = [UIColor blackColor];
[bottoneIntelligente.titleLabel setShadowOffset:CGSizeMake(1.0f, 1.0f)];
[bottoneIntelligente setBackgroundImage: [UIImage imageNamed:@"bottoneverde.png"] forState:UIControlStateNormal];
[self incrementaTitoloOnClick :bottoneIntelligente];
[self analizzaBottoni:bottoneIntelligente];
[self aggiornapunteggi];
}
end:
turno = [self whosTurn];
}
turno = [self whosTurn];
numeromosse ++;
}
发布于 2013-04-09 10:49:21
如果我正确理解,您希望在延迟后执行代码的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
//
});
https://stackoverflow.com/questions/15898557
复制相似问题