Skip to content

Sound Action

R2Road edited this page Feb 22, 2021 · 5 revisions

Sound Play with Action ( cocos2d-x 3.17.2 )

#pragma once

#include <new>
#include <string>

#include "2d/CCActionInstant.h"
#include "audio/include/AudioEngine.h"

USING_NS_CC;

class SoundAction : public ActionInstant
{
private:
	SoundAction( const char* sound_path ) : mSoundPath( sound_path )
	{}

	CC_DISALLOW_COPY_AND_ASSIGN( SoundAction );

public:
	static SoundAction* create( const char* sound_path )
	{
		auto ret = new ( std::nothrow ) SoundAction( sound_path );
		if( ret )
		{
			ret->autorelease();
		}
		else
		{
			CC_SAFE_DELETE( ret );
		}

		return ret;
	}

	void update( float time ) override
	{
		ActionInstant::update( time );
		experimental::AudioEngine::play2d( mSoundPath, false, 0.1f );
	}
	SoundAction* reverse() const override
	{
		return this->clone();
	}
	SoundAction* clone() const override
	{
		return create( mSoundPath.c_str() );
	}

private:
	const std::string mSoundPath;
};
Clone this wiki locally