0

I'm trying to achieve the following

  1. User presses a CCMenuItem
  2. Play sound file (I set it as background music so I can check if it is playing)
  3. Disable touch on all CCMenuItem's
  4. Wait for sound file to complete playing
  5. Upon completion of sound file reenable touch on all CCMenuItems

The function I'm using to wait in my model does the following:

- (void) waitForAudio
{
    while ([[SimpleAudioEngine sharedEngine] isBackgroundMusicPlaying]) {
        NSLog(@"Waiting for audio to finish");
    }
}

My view has the following function:

-(void) setPlaygroundAsTouchable:(BOOL)touchable
{
    [_menuItem0 setIsEnabled:touchable];
    [_menuItem1 setIsEnabled:touchable];
    [_menuItem2 setIsEnabled:touchable];

    _menu.isTouchEnabled=touchable;
}

The controller calls and sets the view function when my model starts and ends a sound file NO and YES respectively.

The problem i'm facing is, the touches on my CCMenuItems seems to be queued up during the playing of my audio- which means each touch is registered and processed when the sound file is completed. Is there a way for me to stop touches from queueing up whilst my waitForAudio function is still looping?

2
  • I don't know much about cocos2d, but I do know that you should be able to add a transparent UIView to accept all incoming touches and then remove it when your sound finishes playing. Commented Sep 15, 2011 at 14:12
  • Thanks for the comment, I have been corrected by another cocos2d user about my waitForAudio function blocking the main thread which is why my touches are queued up.
    – Azeworai
    Commented Sep 16, 2011 at 10:34

1 Answer 1

0

the waitForAudio function was blocking the main thread and thus the touches were queued up. It was not the correct way to figure out when a audio file finishes playing. The following code snippet shows the correct way of listening for the completion of the audio file playing (Credit:toadkick Source link.)

[[CDAudioManager sharedManager] setBackgroundMusicCompletionListener:self selector:@selector(musicFinished)];
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"someMusic.mp3" loop:NO];
[self setPlaygroundAsTouchable:NO];

--

-(void)musicFinished
{
    [[CDAudioManager sharedManager] setBackgroundMusicCompletionListener:nil selector:nil];
    [self setPlaygroundAsTouchable:YES];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.