Sign in to follow this  
jevisan

Sound effects mashed up

Recommended Posts

Hi , i'm having a difficulty while trying to play sounds on player input. The character is supposed to move and play a footstep sound while doing so. But the sound is really short and end up restarting the sound every call to update which causes it to sound mashed up. Is there any way i could fix this?

The code isn't really that much relevant but still:

update: function() {
    if (this.cursors.left.isDown) {
            this.player.body.velocity.x = -this.player.speed;
            this.player.animations.play('walk-left');
            this.footstepA.play();
        } 
}

Oh and i would also like to reproduce another footstep sound after playing that one. Like so:

footstepA, footstepB, footstepA, etc.

Share this post


Link to post
Share on other sites

You have to call a function to manage what sound reproduce.

Instead of:

this.footstepA.play();

put a:

soundplay();

 

Then in soundplay you put all the stuff you need to manage your sounds.

Share this post


Link to post
Share on other sites

@Fenopiù - I think the 'update' gives him problems because the sound is playing continuously.

@jevisan - This might look complicated but it's pretty easy to understand. I apologize for the long post but I've felt like you may want a different approach of playing the sounds.

Methods:

  1. sounds will be played like: A-B-A-B etc.
  2. sounds will be played in groups like: ( A-B ) -> ( A-B ) -> ( B-A ) -> ( A-B ) -> ( B-A ) etc.
  3. sounds will be played randomly like so: A-A-A-A-A-A-A-B-B-B-A-A-A-B etc.

Test all 3 and use what you like the most. I recommend the second method as it sounds more naturally.

Necessary:

//in variables
var cursors;
var footstep_var = 0;
var footstep_rnd_var = 0; //remove this if you use the first method
var keyleft_var = 0;

//in create
this.cursors = game.input.keyboard.createCursorKeys();

//in update
update: function () {
    if (this.cursors.left.isDown) {
        keyleft_var = 1;
        
        //include other stuff here
        
        this.footstep_fn();
    } else {
        keyleft_var = 0;
    }
}

'keyleft_var' checks if you still hold the left key or not. (0 no / 1 yes)

 

1st Method:


footstep_fn: function () {
    if (footstep_var == 0) {
        footstep_var = 1;

        footstepA.play();
        footstepA.onStop.addOnce(function () {
            if (keyleft_var == 0) {
                footstep_var = 0;
            } else {
                footstep_var = 2;
                footstepB.play();
                footstepB.onStop.addOnce(function () {
                    footstep_var = 0;
                }, this);
            }
        }, this);
    }
}

This function is executed in 'update'. Here 'footstep_var' changes to 1 if it's 0 (an easy method of running stuff in 'update' only once).

Play sound A -> 'onStop' will check if sound A has been finished playing -> Check if the left key is still held down (if keyleft_var == 0) -> NO? reset 'footstep_var' to 0 -> YES? change 'footstep_var' to 2 so it won't interfere -> Play sound B -> 'onStop' will check if sound B has been finished playing -> reset 'footstep_var' to 0 so the code will run again.

 

2nd Method:

footstep_fn: function () {
    if (footstep_var == 0) {
        footstep_var = 1;

        footstep_rnd_var = Math.round(Math.random());
        console.log(footstep_rnd_var); //watch the console to see the randomness

        switch (footstep_rnd_var) {
            case 0:
                footstepA.play();
                footstepA.onStop.addOnce(function () {
                    if (keyleft_var == 0) {
                        footstep_var = 0;
                    } else {
                        footstep_var = 2;
                        footstepB.play();
                        footstepB.onStop.addOnce(function () {
                            footstep_var = 0;
                        }, this);
                    }
                }, this);
                break;
            case 1:
                footstepB.play();
                footstepB.onStop.addOnce(function () {
                    if (keyleft_var == 0) {
                        footstep_var = 0;
                    } else {
                        footstep_var = 2;
                        footstepA.play();
                        footstepA.onStop.addOnce(function () {
                            footstep_var = 0;
                        }, this);
                    }
                }, this);
                break;
        }
    }
}

Minor changes from the first method.

The variable 'footstep_rnd_var' will be 0 or 1 (randomizes everytime you hold the left key) -> 'switch' checks this random value and if it's:

  • 0 : play A then B (same code from 1st method) (check explanation there)
  • 1 : play B then A (same code from 1st method but reverses footstepA with B) (check explanation there)
 

3rd Method:

footstep_fn: function () {
    if (footstep_var == 0 || footstep_var == 2) { //HERE
        footstep_var = 1;

        footstep_rnd_var = Math.round(Math.random());
        console.log(footstep_rnd_var); //watch the console to see the randomness

        switch (footstep_rnd_var) {
            case 0:
                footstepA.play();
                footstepA.onStop.addOnce(function () {
                    if (keyleft_var == 0) {
                        footstep_var = 0;
                    } else {
                        footstep_var = 2;
                        this.footstep_fn(); //HERE
                    }
                }, this);
                break;
            case 1:
                footstepB.play();
                footstepB.onStop.addOnce(function () {
                    if (keyleft_var == 0) {
                        footstep_var = 0;
                    } else {
                        footstep_var = 2;
                        this.footstep_fn(); //HERE
                    }
                }, this);
                break;
        }
    }
}

Same as the second method. //HERE means changes to the code.

 

Let me know if there's something that you may find confusing.

Good luck!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.