0

私が書いている単純な対戦ゲームについて助けが必要です。私の問題は、ゲームビューからメインメニュービューに切り替えると、ボタンを点灯させたり消灯させたりするループがバックグラウンドで実行されるまで、ゲームが再生され続けることです。ビューを切り替えるとバックグラウンドでライトが再生されます。「メインメニュー」ボタンを押したときにループにブレークを追加しようとしましたが、機能しません。ボタンに「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];

}
4

1 に答える 1

0

別の変数を追加して、スレッドを中断する必要があるかどうかを判断できます。好き :

あなたの.hファイル

Bool flag;

あなたの.mファイル

-(void)yourBackgroundMethod{
if(flag)
 break;
}
于 2012-06-12T03:47:59.653 に答える