Stop a sound instead of all

I’m building a small game that has a background music and other sounds… there is a sound that I want to be played when the user hit a key, but then, when the user release that same key, I wish I could stop playing that sound only, not all sounds. Is there a way to accomplish that?

1 Like

Not that I know of. I asked for this already but I don’t think there was really a response. There might be a way to do it without the built-in functions.

(Full credit goes to pumpkinhead for this)
In key pressed, use this:

project.bgMusic = this.project.getAssetByName(“sound.mp3”);
project.bgMusic.play();

In key released, use this:

project.bgMusic.stop();

You just need to set the sound to variable, then you can use stop()

2 Likes

dude you legend pumpkinhead

thanks bluecake for showing this, maybe i’ll use this to my advantage on a project

2 Likes

i saw the original post, seems like you have trouble looping, i’ll see what results i get.

I found a workaround for the looping thing. Since all of my projects are eventually going to be exported as HTML, I just used a document method to add audio instead. It’s not…really ideal when working inside the wick editor, there are several drawbacks, but when I export the project as HTML everything functions how it should, so I’m satisfied with that for now. That being said, I’ll try to look more into looping sounds in Wick.

Let me try that. Thanks

Have you tried that? It is not working… at least not the way that you put it.

Thank you for your help… It is not what I’m looking for… I was looking for something more similar to what you replied earlier from @pumpkinhead

@Luxapodular, it is hard to believe that the Wick API does not have a stopSound(“sound.mp3”) kind of function… Do you have an unofficial workaround for this?

There’s actually a stopSound function included in the wickengine.js file, it looks like this:

stopSound(id) {
var asset = this.getAssetByName(assetName);

if (!asset) {
console.warn(‘stopSound(): No asset with name: "’ + assetName + ‘"’);
} else if (!(asset instanceof Wick.SoundAsset)) {
console.warn(‘stopSound(): Asset is not a sound: "’ + assetName + ‘"’);
} else {
return asset.stop(id);
}
}

It just…doesn’t actually work in the editor. By the way, are you saying that the keypressed/keyreleased thing doesn’t work for you? (I thought you were asking about HTML audio)

Sorry for the confusion… I never mentioned the html audio under this specific topic.
It is more related to the topic…

It is not the keypress part… it is more related to start a sound and stop that sound alone.

Ah I’m sorry, the order of the comments confused me ;-;
Personally this worked for me:
stopOneSound.wick (117.4 KB)
(Sound will play when pressing space and should stop when not pressing space)

1 Like

Let me check that… thank you @bluecake.

It is working… It seems that It should be associated to a frame object, not a clip object. That is why it was not working for me before… @bluecake and @pumpkinhead… Thank you so much!

1 Like

Actually, since you are not specifying any key under the keypress tab, it sounds when you press any key.

1 Like

You’re right, I must’ve imagined I typed the if(key===“space”) part (I think because that’s what I typed earlier when I was just testing it out to see if it worked). But I forgot it this time. On a side note, I didn’t actually realize that KeyPressed works for any key if you don’t specify a key, I thought you had to specify a key for it to work…well, I’m glad I learned that at least

1 Like