Try putting externint Sound; in the header file. You want it to always refer to the Sound variable in that particular cpp file even when referenced from another cpp file. The way you have it it will create a new (different) Sound variable in every cpp file that includes the header.
But then again, you probably would've gotten some linker errors...
I was thinking about another situation entirely. Forget what I said.
Instead, it looks like you have an int as a class member that you assign to in one of the member functions. But when you assign to it you are creating a new (different) Sound variable that disappears when it goes out of scope. Then in another member function you are trying to turn the sound off using the value in the original Sound variable, which contains an arbitrary value.
To fix it, just remove the "int" from the assignment statement so you are assigning to the Sound member variable and not a local function variable that is shadowing it.
Is the value of the int returned from cocos2d::experimental::AudioEngine::play2d definitely the same value that you're passing to cocos2d::experimental::AudioEngine::stop?
Wait, it did, sorry, my code is really lengthy, so that was just human error. This is not the end though. We know how to pause the audio, but, ok, let me explain.
context, I want to delay a sound by 0.8 seconds
This was the original bad cpp, more lines of it
1 2 3 4 5 6 7 8 9
auto sound = []()
{
int Sound = cocos2d::experimental::AudioEngine::play2d("Sound.mp3");
};
auto seq = Sequence::create(DelayTime::create(0.8), CallFunc::create(sound), nullptr);
[...]->runAction(seq);
When I edited it with @dutch 's help, I was forced to do this, (to test if it works)
1 2 3 4 5 6
Sound = cocos2d::experimental::AudioEngine::play2d("sound.mp3");
auto sound = []()
{
};
Because unfortunately, If I do
1 2 3 4 5 6
auto sound = []()
{
Sound = cocos2d::experimental::AudioEngine::play2d("sound.mp3");
};
It will say: "the enclosing funtion 'this' cannot be referenceeeeeeeee... Oh, solved it haha, i just had to do
1 2 3 4 5 6
auto sound = [this]()
{
Sound = cocos2d::experimental::AudioEngine::play2d("sound.mp3");
};