tags:

views:

659

answers:

2

Hi All,

I am trying to the restart the same level of the game once it is completed. I am using alert view to display at the time of Finish. I'm using 2 buttons in AlertView (End and PlayAgain). Once end is touched it should end that is working fine. The problem is once playAgain is touched The AlertView is displayed once again but i need to restart the game. The code for button index of AlertView is as shown below.

-(void)alertView :(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
    if(buttonIndex==0){
        exit(0);
        NSLog(@"exit");
    }
    else if(buttonIndex==1){

       // [self retain];
        GameScene *gameScene = [GameScene node];
        [[Director sharedDirector] replaceScene:gameScene];

        NSLog(@"Play Again");
    }
}

Here I am trying to load my gamescene once again. Is it the right way to do.

Please help me on this.

Thanks in Advance,

A: 

As far as I can see, there is nothing wrong with your code to load the new (same scene). This is also the method I am using to switch to the same scene again.

Is your code in the "else if" statement actually hit? What happens if you place a breakpoint in the else if block?

Wim Haanstra
A: 

Hey, the EXACT same thing was happening to me. For me, it turned out I had a scheduler that was happening every frame and it would happen twice before the message actually appeared. I simply used:

[self unschedule:@selector(aFunction:)];

This unscheduled the selector so it wouldn't register twice. I hope this helps someone! If this is not the case, you could maybe try:

[[CCDirector sharedDirector] pause] ;

In hopes of pausing the scene to stop it from calling for the alert twice.

Scott Pfeil