切换视图时如何中断for循环?

时间:2012-06-12 00:31:43

标签: iphone audio ios5 for-loop break

我正在撰写一个简单的比赛游戏,我需要帮助。我的问题是,当我从游戏视图切换到主菜单视图时,游戏一直在播放,直到循环完成在后台运行,点亮按钮并按顺序取消它们,我仍然可以听到它在按钮时发出的声音当我切换视图时,在后台播放灯光。当我按下“主菜单”按钮时,我尝试在循环中添加一个中断,但它不起作用,我标记了按钮'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];

}

1 个答案:

答案 0 :(得分:0)

您可以添加另一个var来确定线程是否应该中断。 喜欢:

你的.h文件

Bool flag;

你的.m文件

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