2012-06-12 3 views
0

Мне нужна помощь с простой игрой матча, которую я пишу. Моя проблема заключается в том, что когда я переключаюсь с игрового режима на главное меню, игра продолжает играть до тех пор, пока цикл не будет запущен в фоновом режиме, который загорается кнопками и сразу отключает их. Я все еще слышу звук, который он делает, когда кнопка когда я переключаю виды. Я попытался добавить разрыв в цикле, когда я нажимаю кнопку «главного меню», но он не работает, я пометил кнопку «100». Ниже приведен соответствующий код, который я написал, имейте в виду, что я все еще новичок, поэтому любые рекомендации будут очень благодарны! Заранее спасибо!Как отключить цикл for при переключении просмотров?

- (IBAction)newGame:(id)sender 
{ 
//creat random array of buttons 
[self randomArray]; 

//initialize button pushed array 
buttonPushedArray = [[NSMutableArray alloc] initWithArray:NULL]; 

//initialize points 
points = [[NSNumber alloc] initWithInt: 30]; 

//Initialize label to show score 
scoreView.text = [NSString stringWithFormat:@"%@", score]; 

//Initialize label to show level 
levelView.text = [NSString stringWithFormat:@"%@", levelNum]; 

//button pushed 
//int a = [sender tag]; 

//loop through each button in array and turn them on and off 
for (NSString *i in gameArray) 
{ 
    if ([sender tag] > 99) 
    { 
     break; 
    }else 
    { 
    int butNum = [i intValue]; 
    delay2 = [delay intValue]; 

    NSNumber *number = [[NSNumber alloc] initWithInt:butNum]; 

    //turn button on 
    [self buttonLit:(NSNumber*)number]; 

    //turn button off 
    [self performSelector:@selector(buttonUnLit:) withObject:number afterDelay:delay2]; 

    //time delay that give 'turn button off' command enough time to respond 
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow: delay2]]; 

    [number release]; 
    } 
}  

} 

- (void)randomArray 
{ 
//initialize array 
gameArray = [[NSMutableArray alloc] initWithArray:NULL]; 

int level = [levelNum intValue]; 

//************* Build random array of numbers ******************* 
for (int i = 0; i < level; i++) 
{ 
    //generate a number from 0 to 11 at random 
    NSInteger num = (arc4random() % 11); 

    //add number to array 
    [gameArray addObject:[NSString stringWithFormat:@"%i", num]]; 
} 
return; 

} 

- (void)buttonLit:(NSNumber*)number 
{ 
int numLitInt = [number intValue]; 


if (numLitInt == 0) 
{ 
    //light up the button 
    [button0 setImage:[UIImage imageNamed:@"0(lite).png"] forState:UIControlStateNormal]; 

    //*********** play audio file **************** 
    NSString* resourcePath = [[NSBundle mainBundle] resourcePath]; 
    resourcePath = [resourcePath stringByAppendingString:@"/ding1.m4a"]; 
    NSError* err; 

    //Initialize our player pointing to the path to our resource 
    player = [[AVAudioPlayer alloc] initWithContentsOfURL: 
       [NSURL fileURLWithPath:resourcePath] error:&err]; 

    if(err){ 
     //bail! 
     NSLog(@"Failed with reason: %@", [err localizedDescription]); 
    } 
    else{ 
     //set our delegate and begin playback 
     [player play]; 
    } 
    return; 
}else if (numLitInt == 1) 
{ 
    //light up the button 
    [button1 setImage:[UIImage imageNamed:@"1(lite).png"] forState:UIControlStateNormal]; 

    //*********** play audio file **************** 
    NSString* resourcePath = [[NSBundle mainBundle] resourcePath]; 
    resourcePath = [resourcePath stringByAppendingString:@"/ding2.m4a"]; 
    NSError* err; 

    //Initialize our player pointing to the path to our resource 
    player = [[AVAudioPlayer alloc] initWithContentsOfURL: 
       [NSURL fileURLWithPath:resourcePath] error:&err]; 

    if(err){ 
     //bail! 
     NSLog(@"Failed with reason: %@", [err localizedDescription]); 
    } 
    else{ 
     //set our delegate and begin playback 
     [player play]; 
    } 
    return; 
}else if (numLitInt == 2) 
{ 
    //light up the button 
    [button2 setImage:[UIImage imageNamed:@"2(lite).png"] forState:UIControlStateNormal]; 

    //*********** play audio file **************** 
    NSString* resourcePath = [[NSBundle mainBundle] resourcePath]; 
    resourcePath = [resourcePath stringByAppendingString:@"/ding3.m4a"]; 
    NSError* err; 

    //Initialize our player pointing to the path to our resource 
    player = [[AVAudioPlayer alloc] initWithContentsOfURL: 
       [NSURL fileURLWithPath:resourcePath] error:&err]; 

    if(err){ 
     //bail! 
     NSLog(@"Failed with reason: %@", [err localizedDescription]); 
    } 
    else{ 
     //set our delegate and begin playback 
     [player play]; 
    } 
    return; 
}else if (numLitInt == 3) 
{ 
    //light up the button 
    [button3 setImage:[UIImage imageNamed:@"3(lite).png"] forState:UIControlStateNormal]; 

    //*********** play audio file **************** 
    NSString* resourcePath = [[NSBundle mainBundle] resourcePath]; 
    resourcePath = [resourcePath stringByAppendingString:@"/ding4.m4a"]; 
    NSError* err; 

    //Initialize our player pointing to the path to our resource 
    player = [[AVAudioPlayer alloc] initWithContentsOfURL: 
       [NSURL fileURLWithPath:resourcePath] error:&err]; 

    if(err){ 
     //bail! 
     NSLog(@"Failed with reason: %@", [err localizedDescription]); 
    } 
    else{ 
     //set our delegate and begin playback 
     [player play]; 
    } 
    return; 
}else if (numLitInt == 4) 
{ 
    //light up the button 
    [button4 setImage:[UIImage imageNamed:@"4(lite).png"] forState:UIControlStateNormal]; 

    //*********** play audio file **************** 
    NSString* resourcePath = [[NSBundle mainBundle] resourcePath]; 
    resourcePath = [resourcePath stringByAppendingString:@"/ding1.m4a"]; 
    NSError* err; 

    //Initialize our player pointing to the path to our resource 
    player = [[AVAudioPlayer alloc] initWithContentsOfURL: 
       [NSURL fileURLWithPath:resourcePath] error:&err]; 

    if(err){ 
     //bail! 
     NSLog(@"Failed with reason: %@", [err localizedDescription]); 
    } 
    else{ 
     //set our delegate and begin playback 
     [player play]; 
    } 
    return; 
}else if (numLitInt == 5) 
{ 
    //light up the button 
    [button5 setImage:[UIImage imageNamed:@"5(lite).png"] forState:UIControlStateNormal]; 

    //*********** play audio file **************** 
    NSString* resourcePath = [[NSBundle mainBundle] resourcePath]; 
    resourcePath = [resourcePath stringByAppendingString:@"/ding2.m4a"]; 
    NSError* err; 

    //Initialize our player pointing to the path to our resource 
    player = [[AVAudioPlayer alloc] initWithContentsOfURL: 
       [NSURL fileURLWithPath:resourcePath] error:&err]; 

    if(err){ 
     //bail! 
     NSLog(@"Failed with reason: %@", [err localizedDescription]); 
    } 
    else{ 
     //set our delegate and begin playback 
     [player play]; 
    } 
    return; 
}else if (numLitInt == 6) 
{ 
    //light up the button 
    [button6 setImage:[UIImage imageNamed:@"6(lite).png"] forState:UIControlStateNormal]; 

    //*********** play audio file **************** 
    NSString* resourcePath = [[NSBundle mainBundle] resourcePath]; 
    resourcePath = [resourcePath stringByAppendingString:@"/ding3.m4a"]; 
    NSError* err; 

    //Initialize our player pointing to the path to our resource 
    player = [[AVAudioPlayer alloc] initWithContentsOfURL: 
       [NSURL fileURLWithPath:resourcePath] error:&err]; 

    if(err){ 
     //bail! 
     NSLog(@"Failed with reason: %@", [err localizedDescription]); 
    } 
    else{ 
     //set our delegate and begin playback 
     [player play]; 
    } 
    return; 
}else if (numLitInt == 7) 
{ 
    //light up the button 
    [button7 setImage:[UIImage imageNamed:@"7(lite).png"] forState:UIControlStateNormal]; 

    //*********** play audio file **************** 
    NSString* resourcePath = [[NSBundle mainBundle] resourcePath]; 
    resourcePath = [resourcePath stringByAppendingString:@"/ding4.m4a"]; 
    NSError* err; 

    //Initialize our player pointing to the path to our resource 
    player = [[AVAudioPlayer alloc] initWithContentsOfURL: 
       [NSURL fileURLWithPath:resourcePath] error:&err]; 

    if(err){ 
     //bail! 
     NSLog(@"Failed with reason: %@", [err localizedDescription]); 
    } 
    else{ 
     //set our delegate and begin playback 
     [player play]; 
    } 
    return; 
}else if (numLitInt == 8) 
{ 
    //light up the button 
    [button8 setImage:[UIImage imageNamed:@"8(lite).png"] forState:UIControlStateNormal]; 

    //*********** play audio file **************** 
    NSString* resourcePath = [[NSBundle mainBundle] resourcePath]; 
    resourcePath = [resourcePath stringByAppendingString:@"/ding1.m4a"]; 
    NSError* err; 

    //Initialize our player pointing to the path to our resource 
    player = [[AVAudioPlayer alloc] initWithContentsOfURL: 
       [NSURL fileURLWithPath:resourcePath] error:&err]; 

    if(err){ 
     //bail! 
     NSLog(@"Failed with reason: %@", [err localizedDescription]); 
    } 
    else{ 
     //set our delegate and begin playback 
     [player play]; 
    } 
    return; 
}else if (numLitInt == 9) 
{ 
    //light up the button 
    [button9 setImage:[UIImage imageNamed:@"9(lite).png"] forState:UIControlStateNormal]; 

    //*********** play audio file **************** 
    NSString* resourcePath = [[NSBundle mainBundle] resourcePath]; 
    resourcePath = [resourcePath stringByAppendingString:@"/ding2.m4a"]; 
    NSError* err; 

    //Initialize our player pointing to the path to our resource 
    player = [[AVAudioPlayer alloc] initWithContentsOfURL: 
       [NSURL fileURLWithPath:resourcePath] error:&err]; 

    if(err){ 
     //bail! 
     NSLog(@"Failed with reason: %@", [err localizedDescription]); 
    } 
    else{ 
     //set our delegate and begin playback 
     [player play]; 
    } 
    return; 
}else if (numLitInt == 10) 
{ 
    //light up the button 
    [button10 setImage:[UIImage imageNamed:@"10(lite).png"] forState:UIControlStateNormal]; 

    //*********** play audio file **************** 
    NSString* resourcePath = [[NSBundle mainBundle] resourcePath]; 
    resourcePath = [resourcePath stringByAppendingString:@"/ding3.m4a"]; 
    NSError* err; 

    //Initialize our player pointing to the path to our resource 
    player = [[AVAudioPlayer alloc] initWithContentsOfURL: 
       [NSURL fileURLWithPath:resourcePath] error:&err]; 

    if(err){ 
     //bail! 
     NSLog(@"Failed with reason: %@", [err localizedDescription]); 
    } 
    else{ 
     //set our delegate and begin playback 
     [player play]; 
    } 
    return; 
}else if (numLitInt == 11) 
{ 
    //light up the button 
    [button11 setImage:[UIImage imageNamed:@"11(lite).png"] forState:UIControlStateNormal]; 

    //*********** play audio file **************** 
    NSString* resourcePath = [[NSBundle mainBundle] resourcePath]; 
    resourcePath = [resourcePath stringByAppendingString:@"/ding4.m4a"]; 
    NSError* err; 

    //Initialize our player pointing to the path to our resource 
    player = [[AVAudioPlayer alloc] initWithContentsOfURL: 
       [NSURL fileURLWithPath:resourcePath] error:&err]; 

    if(err){ 
     //bail! 
     NSLog(@"Failed with reason: %@", [err localizedDescription]); 
    } 
    else{ 
     //set our delegate and begin playback 
     [player play]; 
    } 
    return; 
} 

} 

- (void)buttonUnLit:(NSNumber*)number 
{ 
int numLitInt = [number intValue]; 


if (numLitInt == 0) 
{ 
    [button0 setImage:[UIImage imageNamed:@"0.png"] forState:UIControlStateNormal]; 
    return; 
}else if (numLitInt == 1) 
{ 
    [button1 setImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal]; 
    return; 
}else if (numLitInt == 2) 
{ 
    [button2 setImage:[UIImage imageNamed:@"2.png"] forState:UIControlStateNormal]; 
    return; 
}else if (numLitInt == 3) 
{ 
    [button3 setImage:[UIImage imageNamed:@"3.png"] forState:UIControlStateNormal]; 
    return; 
}else if (numLitInt == 4) 
{ 
    [button4 setImage:[UIImage imageNamed:@"4.png"] forState:UIControlStateNormal]; 
    return; 
}else if (numLitInt == 5) 
{ 
    [button5 setImage:[UIImage imageNamed:@"5.png"] forState:UIControlStateNormal]; 
    return; 
}else if (numLitInt == 6) 
{ 
    [button6 setImage:[UIImage imageNamed:@"6.png"] forState:UIControlStateNormal]; 
    return; 
}else if (numLitInt == 7) 
{ 
    [button7 setImage:[UIImage imageNamed:@"7.png"] forState:UIControlStateNormal]; 
    return; 
}else if (numLitInt == 8) 
{ 
    [button8 setImage:[UIImage imageNamed:@"8.png"] forState:UIControlStateNormal]; 
    return; 
}else if (numLitInt == 9) 
{ 
    [button9 setImage:[UIImage imageNamed:@"9.png"] forState:UIControlStateNormal]; 
    return; 
}else if (numLitInt == 10) 
{ 
    [button10 setImage:[UIImage imageNamed:@"10.png"] forState:UIControlStateNormal]; 
    return; 
}else if (numLitInt == 11) 
{ 
    [button11 setImage:[UIImage imageNamed:@"11.png"] forState:UIControlStateNormal]; 
    return; 
} 

} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
[super viewWillDisappear:animated]; 
[player stop]; 

} 

ответ

0

Вы можете добавить еще один var, чтобы определить, что нить должна быть разбита. как:

ваш .h файл

Bool flag; 

файл .m

-(void)yourBackgroundMethod{ 
if(flag) 
break; 
} 
+0

Спасибо за предложение, я ничего пытался не работал, так что я установить время задержки между итераций в цикле для ноль при переключении экранов и очень быстро заканчивает цикл во время перехода. Не очень элегантный, но он работает :) – user961632

Смежные вопросы