{"id":255,"date":"2014-02-07T18:19:51","date_gmt":"2014-02-07T18:19:51","guid":{"rendered":"http:\/\/fuguelike.com\/?p=255"},"modified":"2014-02-07T18:22:23","modified_gmt":"2014-02-07T18:22:23","slug":"music-fader-for-cocos2dx","status":"publish","type":"post","link":"https:\/\/fuguelike.com\/blog\/music-fader-for-cocos2dx\/","title":{"rendered":"Music Fader for Cocos2dx"},"content":{"rendered":"<p>Here&#8217;s a class for fading music in Cocos2dx 2.x using the SimpleAudioEngine. Seems like this should be a basic function of the SimpleAudioEngine itself, but this is pretty easy to use, just run it on any CCNode like any other action:<\/p>\n<pre class=\"prettyprint linenums\">runAction(MusicFade::create(1.0f, 0.0f, true));<\/pre>\n<p>Copy and paste from below, or <a href=\"http:\/\/fuguelike.com\/music-fader-for-cocos2dx\/musicfade\/\" rel=\"attachment wp-att-261\">here&#8217;s a zip<\/a> with the files.<\/p>\n<pre class=\"prettyprint linenums\">#include \"MusicFade.h\"\r\n#include \"SimpleAudioEngine.h\"\r\n\r\nusing namespace cocos2d;\r\nusing namespace CocosDenshion;\r\n\r\nMusicFade::MusicFade()\r\n{\r\n    m_initialVal = 0;\r\n    m_targetVal = 0;\r\n}\r\n\r\nMusicFade* MusicFade::create(float duration, float volume, bool pauseOnComplete)\r\n{\r\n    MusicFade *pAction = new MusicFade();\r\n    pAction-&gt;initWithDuration(duration, volume, pauseOnComplete);\r\n    pAction-&gt;autorelease();\r\n\r\n    return pAction;\r\n}\r\n\r\nbool MusicFade::initWithDuration(float duration, float volume, bool pauseOnComplete)\r\n{\r\n    if (CCActionInterval::initWithDuration(duration))\r\n    {\r\n        m_targetVal = volume;\r\n        m_bPauseOnComplete = pauseOnComplete;\r\n        return true;\r\n    }\r\n\r\n    return false;\r\n}\r\n\r\nvoid MusicFade::update(float time)\r\n{\r\n    float vol = m_initialVal + time*(m_targetVal - m_initialVal);\r\n    SimpleAudioEngine::sharedEngine()-&gt;setBackgroundMusicVolume(vol);\r\n\r\n}\r\n\r\nvoid MusicFade::startWithTarget(CCNode *pTarget)\r\n{\r\n    CCActionInterval::startWithTarget( pTarget );\r\n    m_initialVal = SimpleAudioEngine::sharedEngine()-&gt;getBackgroundMusicVolume();\r\n}\r\n\r\nvoid MusicFade::stop(void)\r\n{\r\n    if(m_bPauseOnComplete) SimpleAudioEngine::sharedEngine()-&gt;pauseBackgroundMusic();\r\n    CCActionInterval::stop();\r\n}<\/pre>\n<pre class=\"prettyprint linenums\">#ifndef __MusicFade__\r\n#define __MusicFade__\r\n\r\n#include \"cocos2d.h\"\r\n\r\nclass MusicFade : public cocos2d::CCActionInterval\r\n{\r\npublic:\r\n    MusicFade();\r\n\r\n    static MusicFade* create(float d, float volume, bool pauseOnComplete = false );\r\n    bool initWithDuration(float d, float volume, bool pauseOnComplete );\r\n\r\n    virtual void startWithTarget(cocos2d::CCNode *pTarget);\r\n    virtual void update(float time);\r\n    virtual void stop(void);\r\n\r\nprotected:\r\n    float m_targetVal;\r\n    float m_initialVal;\r\n    bool m_bPauseOnComplete;\r\n};\r\n\r\n#endif \/* defined(__MusicFade__) *\/<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a class for fading music in Cocos2dx 2.x using the SimpleAudioEngine. Seems like this should be a basic function of the SimpleAudioEngine itself, but this is pretty easy to use, just run it on any CCNode like any other action: runAction(MusicFade::create(1.0f, 0.0f, true)); Copy and paste from below, or here&#8217;s a zip with the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Music Fader for Cocos2dx http:\/\/wp.me\/p2YXmV-47","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-255","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2YXmV-47","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/fuguelike.com\/blog\/wp-json\/wp\/v2\/posts\/255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fuguelike.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fuguelike.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fuguelike.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fuguelike.com\/blog\/wp-json\/wp\/v2\/comments?post=255"}],"version-history":[{"count":6,"href":"https:\/\/fuguelike.com\/blog\/wp-json\/wp\/v2\/posts\/255\/revisions"}],"predecessor-version":[{"id":263,"href":"https:\/\/fuguelike.com\/blog\/wp-json\/wp\/v2\/posts\/255\/revisions\/263"}],"wp:attachment":[{"href":"https:\/\/fuguelike.com\/blog\/wp-json\/wp\/v2\/media?parent=255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fuguelike.com\/blog\/wp-json\/wp\/v2\/categories?post=255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fuguelike.com\/blog\/wp-json\/wp\/v2\/tags?post=255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}