Where can I choose to stop playing background music? For example, in the helloworld scenario, the main code is as follows:
[HTML]View plaincopy
- Bool helloworld: Init ()
- {
- Return true;
- }
- Void helloworld: onenter ()
- {
- Layer: onenter ();
- Log ("helloworldonenter ");
- }
- Voidhelloworld: onentertransitiondidfinish ()
- {
- Layer: onentertransitiondidfinish ();
- Log ("helloworldonentertransitiondidfinish ");
- }
- Void helloworld: onexit ()
- {
- Layer: onexit ();
- Log ("helloworldonexit ");
- // Code ①
- }
- Voidhelloworld: onexittransitiondidstart ()
- {
- Layer: onexittransitiondidstart ();
- Log ("helloworldonexittransitiondidstart ");
- // Code ②
- }
- Void helloworld: cleanup ()
- {
- Layer: cleanup ();
- Log ("helloworldcleanup ");
- // Code ③
- }
In theory, we can stop playing background music with the following code: simpleaudioengine: getinstance ()-> stopbackgroundmusic ("sound/jazzaudio ") place it in three locations (①, ②, and ③ In the code ). Next we will analyze their differences separately.
1. Place the code in rows ① and ②.
Put the code in rows ① and ② (that is, in the helloworld: init function). If you call the background music function in a later scenario, the background music is abnormal. We have discussed this issue in the previous section.
2. Place the Code in line ③
Put the code in row ③ (that is, in the helloworld: cleanup function). This function is called when the layer object is cleared. It is better to stop playing background music here.
More content please pay attention to the Cocos2d-x series of books "Cocos2d-x practice (Volume I): c ++ development" book exchange discussion site: http://www.cocoagame.net welcome to join the cocos2d-x Technology Discussion Group: 257760386, 327403678