Solve the exception when cocos2dx exits using SimpleAudioEngine Library

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.