感谢您花时间看这个问题,我对编程非常陌生并且遇到以下错误的问题。任何帮助都会非常感激。
我目前正在学习Ray Wenderlichs' Learning Cocos2D'预订,但也尝试使用代码来尝试了解更多,这是我遇到无法修复的问题。
我正在使用CocosDenshion播放主菜单和第一个游戏场景的音乐曲目,Ray在他的书中建议从GAMEPLAY SCENE中调用以下方法......
[[GameManager sharedGameManager] playBackgroundTrack:BACKGOUND_TRACK_OLE_AWAKES];
这当然有效但不是在游戏场景中调用上述方法,而是想控制GameManager类中音乐的开始和停止。 所以我想要在菜单中播放音乐,然后在游戏玩法层上播放任何内容并从一个类中控制它。
为此,我尝试在" GameManager.h中添加以下内容。"。
if (soundEngine.isBackgroundMusicPlaying == YES) {
[soundEngine stopBackgroundMusic];
}
进入下面的方法......
- (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;
现在,当我运行MAIN MENU和OPTIONS屏幕时,上面的工作正常,但是当我尝试加载GAMELEVEL1屏幕时似乎崩溃了。崩溃似乎只是偶尔发生一次,这至少令人困惑。
当它崩溃时,它会转到CDAudioManager.h中的以下内容。
-(BOOL) isPlaying {
if (state != kLAS_Init) {
return [audioSourcePlayer isPlaying]; <--Thread 18:EXC_BAD_ACCESS(code=1,address=0x8001400c)
} else {
return NO;
}
}
我也在控制台中得到以下内容......
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)
很抱歉这个问题很长,但我想尝试尽可能多地提供信息。
修改
我可能完全错了但是如果从2个不同的类调用声音引擎会导致这个吗?正如我在MainMenu层中有以下内容......
-(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;
}