Skip to content

Commit

Permalink
Added MusicTo action for fade in/fade out music effects
Browse files Browse the repository at this point in the history
  • Loading branch information
joni-mikkola committed Jan 25, 2015
1 parent 47ad6b0 commit 2dc80c0
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Classes/HelloWorldScene.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"

USING_NS_CC;

Expand Down Expand Up @@ -77,6 +78,14 @@ bool HelloWorld::init() {

scheduleUpdate();

//Sample audio from: http://freesound.org/people/FREDIOHEAD/sounds/170288/
//Example for using MusicTo
//Fade in and fade out
CocosDenshion::SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(0.0f);
CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("music/drumbeat.mp3");
Sequence* fadeMusicInOutSequence = Sequence::create(MusicTo::create(4.0f, 0.7f), MusicTo::create(4.0f, 0.0f), NULL);
runAction(fadeMusicInOutSequence);

return true;
}

Expand Down
Binary file added Resources/music/drumbeat.mp3
Binary file not shown.
44 changes: 44 additions & 0 deletions cocos2d/cocos/2d/CCActionInterval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ THE SOFTWARE.
#include "base/CCEventDispatcher.h"
#include "platform/CCStdC.h"

#include "SimpleAudioEngine.h"

NS_CC_BEGIN

// Extra action for making a Sequence or Spawn when only adding one action to it.
Expand Down Expand Up @@ -2110,6 +2112,48 @@ DelayTime* DelayTime::reverse() const
return DelayTime::create(_duration);
}

//
// MusicTo
//

MusicTo* MusicTo::create(float duration, float volume)
{
MusicTo *musicTo = new (std::nothrow) MusicTo();
musicTo->initWithDuration(duration, volume);
musicTo->autorelease();

return musicTo;
}

bool MusicTo::initWithDuration(float duration, float volume)
{
if (ActionInterval::initWithDuration(duration))
{
_toVolume = volume;
return true;
}

return false;
}

void MusicTo::startWithTarget(Node *target)
{
ActionInterval::startWithTarget(target);

if (target)
{
_fromVolume = CocosDenshion::SimpleAudioEngine::getInstance()->getBackgroundMusicVolume();
}
}

void MusicTo::update(float time)
{
if (_target)
{
CocosDenshion::SimpleAudioEngine::getInstance()->setBackgroundMusicVolume((float)(_fromVolume + (_toVolume - _fromVolume) * time));
}
}

//
// ReverseTime
//
Expand Down
26 changes: 26 additions & 0 deletions cocos2d/cocos/2d/CCActionInterval.h
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,32 @@ class CC_DLL DelayTime : public ActionInterval
CC_DISALLOW_COPY_AND_ASSIGN(DelayTime);
};


/** @brief Changes music volume level
*/
class CC_DLL MusicTo : public ActionInterval
{
public:
/** creates the action */
static MusicTo* create(float d, float );

//
// Overrides
//
virtual void startWithTarget(Node *target) override;
virtual void update(float time) override;

bool initWithDuration(float duration, float volume);
CC_CONSTRUCTOR_ACCESS:
MusicTo() {};
virtual ~MusicTo() {}
private:
CC_DISALLOW_COPY_AND_ASSIGN(MusicTo);
float _toVolume;
float _fromVolume;
};


/** @brief Executes an action in reverse order, from time=duration to time=0
@warning Use this action carefully. This action is not
Expand Down
6 changes: 6 additions & 0 deletions proj.ios_mac/Tutorials.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
521A8E6519F0C34300D177D7 /* Default-736h@3x.png in Resources */ = {isa = PBXBuildFile; fileRef = 521A8E6319F0C34300D177D7 /* Default-736h@3x.png */; };
521A8EA919F11F5000D177D7 /* fonts in Resources */ = {isa = PBXBuildFile; fileRef = 521A8EA819F11F5000D177D7 /* fonts */; };
521A8EAA19F11F5000D177D7 /* fonts in Resources */ = {isa = PBXBuildFile; fileRef = 521A8EA819F11F5000D177D7 /* fonts */; };
95F05CD51A75793E00E84285 /* music in Resources */ = {isa = PBXBuildFile; fileRef = 95F05CD41A75793E00E84285 /* music */; };
95F05CD61A75793E00E84285 /* music in Resources */ = {isa = PBXBuildFile; fileRef = 95F05CD41A75793E00E84285 /* music */; };
95F7B1971A264C590029576C /* ShadowLayer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 95F7B1951A264C590029576C /* ShadowLayer.cpp */; };
95F7B1981A264C590029576C /* ShadowLayer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 95F7B1951A264C590029576C /* ShadowLayer.cpp */; };
BF171245129291EC00B8313A /* OpenGLES.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BF170DB012928DE900B8313A /* OpenGLES.framework */; };
Expand Down Expand Up @@ -151,6 +153,7 @@
521A8E6219F0C34300D177D7 /* Default-667h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-667h@2x.png"; sourceTree = "<group>"; };
521A8E6319F0C34300D177D7 /* Default-736h@3x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-736h@3x.png"; sourceTree = "<group>"; };
521A8EA819F11F5000D177D7 /* fonts */ = {isa = PBXFileReference; lastKnownFileType = folder; path = fonts; sourceTree = "<group>"; };
95F05CD41A75793E00E84285 /* music */ = {isa = PBXFileReference; lastKnownFileType = folder; path = music; sourceTree = "<group>"; };
95F7B1951A264C590029576C /* ShadowLayer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ShadowLayer.cpp; sourceTree = "<group>"; };
95F7B1961A264C590029576C /* ShadowLayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ShadowLayer.h; sourceTree = "<group>"; };
BF170DB012928DE900B8313A /* OpenGLES.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenGLES.framework; path = System/Library/Frameworks/OpenGLES.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -275,6 +278,7 @@
isa = PBXGroup;
children = (
521A8EA819F11F5000D177D7 /* fonts */,
95F05CD41A75793E00E84285 /* music */,
3EACC98E19EE6D4300EB3C5E /* res */,
46880B7619C43A67006E1F66 /* CloseNormal.png */,
46880B7719C43A67006E1F66 /* CloseSelected.png */,
Expand Down Expand Up @@ -451,6 +455,7 @@
50EF629917ECD46A001EB2F8 /* Icon-100.png in Resources */,
5087E78317EB970100C73F5D /* Icon-152.png in Resources */,
46880B8119C43A67006E1F66 /* HelloWorld.png in Resources */,
95F05CD51A75793E00E84285 /* music in Resources */,
46880B7D19C43A67006E1F66 /* CloseSelected.png in Resources */,
5087E77D17EB970100C73F5D /* Default-568h@2x.png in Resources */,
5087E78517EB970100C73F5D /* Icon-72.png in Resources */,
Expand Down Expand Up @@ -480,6 +485,7 @@
3EACC99019EE6D4300EB3C5E /* res in Resources */,
521A8EAA19F11F5000D177D7 /* fonts in Resources */,
46880B7C19C43A67006E1F66 /* CloseNormal.png in Resources */,
95F05CD61A75793E00E84285 /* music in Resources */,
46880B7E19C43A67006E1F66 /* CloseSelected.png in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down

0 comments on commit 2dc80c0

Please sign in to comment.