简体   繁体   English

如何在Xcode上的IBAction代码块之后调用方法

[英]How to call a method after IBAction code block on Xcode

I have created an iPhone board game. 我创建了一个iPhone棋盘游戏。 It has the ability to choose if user wants to play multi player or single player. 它具有选择用户要玩多人游戏还是单人游戏的能力。

I've used the UIButton for the pieces in the board. 我已经将UIButton用于板上的零件。

I have got one thing that I want to fix: In Single Player mode, I have implemented AI's turn into UIButton 's IBAction . 我要修复的一件事是:在“单人游戏”模式下,我已将AI转换为UIButtonIBAction But, in this case, there's no time there between player1's move and player2's move. 但是,在这种情况下,玩家1的移动与玩家2的移动之间没有时间。 Both gets executed at the same time. 两者同时执行。

I'd like to do this: Make human's move immediate, and then the computer thinks for his move. 我想这样做:让人们立即行动,然后计算机考虑他的行动。 I don't want both moves come together and after that computer thinks to the move. 我不希望这两个动作同时出现,然后那台计算机考虑这个动作。

I know that the problem is that because the game reads the entire code block in IBAction and then he shows the result, but i don't know how to fix this. 我知道问题在于,因为游戏会读取IBAction中的整个代码块,然后他显示结果,但是我不知道如何解决此问题。

Any suggestion? 有什么建议吗?

If you need it, I can show the code, but I think (and I hope, too) that, you'll understand what i mean. 如果您需要它,我可以显示代码,但是我认为(并且也希望如此),您将理解我的意思。

Because of the comments who requested it, here's the code.. 由于有人提出了要求,因此这里是代码。

-(void)buttonClickedSinglePlayer:(id)sender{ -(void)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 mark Single Player's Action 实用标记单人游戏

    //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 ++;

} }

If I understand correctly, you want to execute the AI section of the code after a delay. 如果我理解正确,则希望在延迟后执行代码的AI部分。 If you can refactor the code into another method, then you can use performSelector:withObject:afterDelay: which would look like: 如果可以将代码重构为其他方法,则可以使用performSelector:withObject:afterDelay:如下所示:

[self performSelector:@selector(aiMove) withObject:nil afterDelay:2.0];

This would run the method after 2 seconds. 这将在2秒钟后运行该方法。 Or you can use GCD's dispatch_after which looks like this (below). 或者,您可以使用GCD的dispatch_after (如下所示)。 Place the code that you want to run after a delay into the block. 延迟后将要运行的代码放入块中。

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
 //

 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM