[SOLVED] Sound Distorted In Launch Window

Hi,

I am trying to play a sound when a button is clicked, and stop the same sound when it is released. My script is supposed to play the sound when the bass drum in my scene in clicked. When I launch, the sound is distorted, and does not play correctly.

EDITOR - https://playcanvas.com/editor/scene/896588
CODE - https://playcanvas.com/editor/code/674640?tabs=29486210

How can I proceed?

Hi @XKRS-GT,

That’s a cool project!

So your issue is that you start playing the sound not only once, but once every frame. You should avoid playing your sounds in your update loop, but move them into functions.

Here is an example based on your script, no need to stop the sound explicitly:

var BassDrum = pc.createScript('bassDrum');

// initialize code called once per entity
BassDrum.prototype.initialize = function() {
    
    this.app.root.findByName('Bass').element.on('mousedown', function (event) {
        
        this.playSound('BassSound');
        
    }, this);
    
};

// update code called every frame
BassDrum.prototype.playSound = function(soundName) {
    
    this.entity.sound.play(soundName);
    
};
2 Likes

Thanks a lot! Will do

1 Like

Hey @Leonidas,

Tried what you said. It works for most of the entities in the scene. However, despite using the same code, we couldn’t get the LowTom, FloorTom, and HighTom to work. They don’t seem to be taking input, despite use input being checked. Could you please take a look?

Some of the scripts names have changed (eg floorTom to floortom). Parse the scripts again and re-add scripts to the entities as needed.

2 Likes

I’ll take a look. Thanks.

1 Like