我需要帮助我正在编写一个简单的比赛游戏。我的问题是,当我从游戏视图切换到主菜单视图时,游戏一直在播放,直到在后台运行一个循环完成,该循环点亮一个按钮并按顺序取消点亮它们我仍然可以听到它在按下按钮时发出的声音当我切换视图时,灯光在后台播放。当我按下“主菜单”按钮时,我尝试在循环中添加一个中断,但它不起作用,我将按钮标记为“100”。以下是我编写的相关代码,请记住我仍然是新手,所以任何建议将不胜感激!先感谢您!
- (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];
}