这就是发生的事...... 我正在制作一款游戏,在挑战结束时,定制的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或者我在这里做错了什么之后挑战才结束?警报显示两次,我觉得自己很傻:(