1. Using SimpleAudioEngine to support sound effects in Cocos2d-x games, it is found that exiting from the system message (that is, the window close button) often causes exceptions;
2. When a menu button is used in the game to exit, the following code is added to the button callback function to exit without exception:
Void GameMenu: menuExitCallback (CCObject * pSender)
{
SimpleAudioEngine: sharedEngine ()-> end (); // This sentence plays a role.
CCDirector: sharedDirector ()-> end ();
}
3. After analysis, add the following code to AppDelegate:
AppDelegate ::~ AppDelegate ()
{
SimpleAudioEngine: sharedEngine ()-> end ();
}
4. analysis process:
When you exit from the system message (that is, the window close button), WindowProc of the CCEGLView class receives the WM_CLOSE message and WM_DESTORY message;
When you exit from a menu button in the game, WindowProc of the CCEGLView class receives the WM_DESTROY message.
CCEGLView: WindowProc ()
{
Case WM_CLOSE:
CCDirector: sharedDirector ()-> end ();
Break;
Case WM_DESTROY:
DestroyGL ();
PostQuitMessage (0 );
Break;
}
Because the processing of the two messages is prior to the exception timing, the first idea is to add SimpleAudioEngine: sharedEngine ()-> end () here ().
After compilation, you will find that the CCEGLView class is in the libcocos2d library. Instead, libCocosDenshion depends on it. If you call the SimpleAudioEngine class method in the CCEGLView class, actually, the libcocos2d library depends on its own library. This is unreasonable.
Further analysis showed that before exiting the system and causing an exception, SimpleAudioEngine: sharedEngine ()-> end () can be called ().
One is the onExit method of the main scene layer, that is, the CCNode: OnExit method of the root node;
One is the application's destructor AppDelegate ::~ AppDelegate ().
In contrast, AppDelegate ::~ Adding SimpleAudioEngine: sharedEngine ()-> end () to AppDelegate () is more universal.
Because the scenario OnEixt may also occur during scenario switching, unless you need to clear all the sound effects and background music that have been loaded in the sound library during scenario switching, otherwise, this is unnecessary.