0

这就是正在发生的事情......我正在制作一个游戏,在挑战结束时,定制的 UIAlert 会显示玩家是赢还是输。问题是......该消息显示两次,我不知道为什么......

-(void)checkOnTests{

int p1sc;
int p1t;
int p2sc;
int p2t;

// IN BETWEEN, SCORES ARE CHECKED AND THEY ARE FINE

if (p1sc > p2sc) {
    NSLog(@"P1 Wins");
    winnerIndex = 1; // (INT)
    if (playerIndex == winnerIndex) {
        self.lbl_plyer_score.textColor = [UIColor greenColor]; //WINS
        self.lbl_opponent_score.textColor = [UIColor redColor]; //LOSES
        [self finishChallengeWherePlayer:YES amtOfGold:2];
    }else{
        self.lbl_plyer_score.textColor = [UIColor redColor];
        self.lbl_opponent_score.textColor = [UIColor greenColor];
        [self finishChallengeWherePlayer:NO amtOfGold:0];
    }
}else if (p1sc < p2sc){
    winnerIndex = 2;
    NSLog(@"P2 Wins");
    if (playerIndex == winnerIndex) {
        self.lbl_plyer_score.textColor = [UIColor greenColor];
        self.lbl_opponent_score.textColor = [UIColor redColor];
        [self finishChallengeWherePlayer:YES amtOfGold:2];

    }else{
        self.lbl_plyer_score.textColor = [UIColor redColor];
        self.lbl_opponent_score.textColor = [UIColor greenColor];
        [self finishChallengeWherePlayer:NO amtOfGold:0];

    }
}else if (p1sc == p2sc){
    NSLog(@"We got a tie match");
    winnerIndex = 3;
    if (((p1t == 0) && (p2t == 0)) || (p1t == p2t)) {
        [self finishChallengeWherePlayer:YES amtOfGold:1];
    }else if (p1t < p2t){
       [self finishChallengeWherePlayer:YES amtOfGold:2]; 
    }else if (p1t > p2t){
        [self finishChallengeWherePlayer:YES amtOfGold:1];
    }

  }
}

上面的代码检查结果。到目前为止一切都是正确的。没有错误。下面我实现了向玩家显示消息的代码,告诉玩家他们是赢还是输。

-(void)finishChallengeWherePlayer:(BOOL)wins amtOfGold:(int)amt {
NSString *endMsg;
if (wins) {
    endMsg = [NSString stringWithFormat:@"Congrats! You win the challenge, gaining: %i gold", amt];
}else{
    endMsg = [NSString stringWithFormat:@"Oh No! You have lost the challenge. No gold for you"];
}
GameAlert *alert = [[GameAlert alloc] initWithTitle:@"Results" message:endMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show]; // this is my custom UIAlertView that is working fine throughout the game.
[self.delegate challengeFinished:amt testname:linker];

}

此视图控制器与游戏其余部分的唯一不同之处在于下面的框中:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
[self dismissViewControllerAnimated:YES completion:nil];
}

我的问题是......我真的必须告诉代表只有在我关闭这个 ViewController 之后挑战才结束,还是我在这里做错了什么?警报显示两次,我觉得自己像个傻瓜:(

4

0 回答 0