Thanks for taking the time to look as this question, I am very new to programming and am having a problem with the following error. Any help would be really appreciated.
I am currently learning via Ray Wenderlichs 'Learning Cocos2D' book but also experiment with the code to attempt to learn more and this is where I have run into a problem I can not fix.
I am using CocosDenshion to play the music tracks for the main menu and first gameplay scene, Ray advises in his book to call the following method from the GAMEPLAY SCENE...
[[GameManager sharedGameManager] playBackgroundTrack:BACKGOUND_TRACK_OLE_AWAKES];
This of course works but rather than call the above method in the gameplay scene I want to control the start and stopping of the music from my GameManager class. So I want music to play in the menu and then nothing to play on the gameplay layer and to control it from one class.
To do this I have attempted to add the following in "GameManager.h.".
if (soundEngine.isBackgroundMusicPlaying == YES) {
[soundEngine stopBackgroundMusic];
}
In to the method below...
- (NSString*)formatSceneTypeToString:(SceneTypes)sceneID {
NSString *result = nil;
switch(sceneID) {
case kNoSceneUninitialized:
if ((soundEngine.isBackgroundMusicPlaying == YES)){
[soundEngine stopBackgroundMusic];
}
result = @"kNoSceneUninitialized";
break;
case kMainMenuScene:
if ((isSoundEffectsOn == YES)&&(soundEngine.isBackgroundMusicPlaying == NO)){
[self playBackgroundTrack:BACKGROUND_TRACK_MAIN_MENU];
} else if ((isSoundEffectsOn == NO)&&(soundEngine.isBackgroundMusicPlaying == YES)){
[soundEngine stopBackgroundMusic];
}
if (isMusicOn == YES) {
result = @"kMainMenuScene";
} else if (isMusicOn == NO) {
result = @"kNoSceneUninitialized";
// return NO;
}
break;
case kOptionsScene:
if ((isSoundEffectsOn == YES)&&(soundEngine.isBackgroundMusicPlaying == NO)){
[self playBackgroundTrack:BACKGROUND_TRACK_MAIN_MENU];
} else if ((isSoundEffectsOn == NO)&&(soundEngine.isBackgroundMusicPlaying == YES)){
[soundEngine stopBackgroundMusic];
}
if (isMusicOn == YES) {
result = @"kOptionsScene";
} else if (isMusicOn == NO) {
// return NO;
result = @"kNoSceneUninitialized";
}
break;
case kGameLevel1:
if ([soundEngine.isBackgroundMusicPlaying == YES]){
[soundEngine stopBackgroundMusic]; <--CRASHES HERE!
} else {
}
result = @"kGameLevel1";
break;
Now the above works fine for the MAIN MENU and OPTIONS screens when I run it but seems to crash when I attempt to load the GAMELEVEL1 screen. The crash only seems to occur once every now and then which is confusing to say the least.
When it crashes it takes me to the following in CDAudioManager.h..
-(BOOL) isPlaying {
if (state != kLAS_Init) {
return [audioSourcePlayer isPlaying]; <--Thread 18:EXC_BAD_ACCESS(code=1,address=0x8001400c)
} else {
return NO;
}
}
I also get the below in the console...
2014-10-28 12:46:03.948 SpaceViking[1807:343] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CCStandardTouchHandler isPlaying]: unrecognized selector sent to instance 0x7e013d80'
*** First throw call stack:
(
0 CoreFoundation 0x05bb25e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x025658b6 objc_exception_throw + 44
2 CoreFoundation 0x05c4f903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x05ba290b ___forwarding___ + 1019
4 CoreFoundation 0x05ba24ee _CF_forwarding_prep_0 + 14
5 SpaceViking 0x00183e72 -[CDLongAudioSource isPlaying] + 82
6 SpaceViking 0x00185425 -[CDAudioManager isBackgroundMusicPlaying] + 69
7 SpaceViking 0x0018ea60 -[SimpleAudioEngine isBackgroundMusicPlaying] + 48
8 SpaceViking 0x0019fe62 -[GameManager formatSceneTypeToString:] + 9634
9 SpaceViking 0x001a095e -[GameManager getSoundEffectsListForSceneWithID:] + 2302
10 SpaceViking 0x0019d50a -[GameManager unloadAudioForSceneWithID:] + 186
11 Foundation 0x01e39597 -[NSThread main] + 76
12 Foundation 0x01e394f6 __NSThread__main__ + 1275
13 libsystem_pthread.dylib 0x02d185fb _pthread_body + 144
14 libsystem_pthread.dylib 0x02d18485 _pthread_struct_init + 0
15 libsystem_pthread.dylib 0x02d1dcf2 thread_start + 34
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
Sorry for the length of this question but wanted to try and include as much information as possible.
EDIT
I could be completely wrong but if the sound engine is called from 2 different classes would that cause this? As i have the following in the MainMenu layer...
-(void)playScene:(CCMenuItemFont*)itemPassedIn {
if ([itemPassedIn tag] == 1) {
CCLOG(@"Tag 1 found, mode");
if ([GameManager sharedGameManager].isSoundEffectsOn ==YES ) {
PLAYSOUNDEFFECT(BUTTONPRESS); <----Calling the sound engine to play a sound on button press.- this is unloading the sound as level1 starts.
[[GameManager sharedGameManager] runSceneWithID:kGameLevel1];
}else if ([GameManager sharedGameManager].isSoundEffectsOn ==NO ) {
[[GameManager sharedGameManager] runSceneWithID:kGameLevel1];
return;
}