Pregunta

How can I create a UIAlertView with some text and a button that will restart the game that I'm creating.

Thanks in advance,

Best Regards, Louis.

¿Fue útil?

Solución

Try this one :

[[CCDirector sharedDirector] pause];
UIAlertView *pauseAlert = [[UIAlertView alloc] initWithTitle:@"Game Paused" message:nil delegate:self cancelButtonTitle:@"CANCEL" otherButtonTitles:@"RESTART", nil];
[pauseAlert show];

Delegate Method..

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
   if (buttonIndex == 0)
   {
      NSLog(@"Cancel..");
   }
   else if (buttonIndex == 1)
   {
      NSLog(@"restart..");
      [[CCDirector sharedDirector] resume];
   }
}

Otros consejos

You need to detect whether a button has been tapped on the UIAlertView. For this, you will need to implement the UIAlertViewDelegate protocol.

Follow these steps:

1 - Implement the protocol. In the scene's .h file

@interface MyScene : SKScene <UIAlertViewDelegate> //This will implement the protocol.

2 - Declare and instantiate the UIAlertView. Set the delegate.

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Restart?" message:@"Do you want to restart the level?" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"Yes", nil];
[alert show];

3 - Implement the delegate method

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if ([alertView.title isEqualToString:@"Restart?"] && buttonIndex == 1)
    {
        //Code for restarting the level here.
    }
}

Please refer to this tutorial for an explanation on how UIAlertView works.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top