jevisan Posted February 14, 2018 Share Posted February 14, 2018 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. Link to comment Share on other sites More sharing options...
Fenopiù Posted February 14, 2018 Share Posted February 14, 2018 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. Link to comment Share on other sites More sharing options...
3man7 Posted February 14, 2018 Share Posted February 14, 2018 @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: sounds will be played like: A-B-A-B etc. sounds will be played in groups like: ( A-B ) -> ( A-B ) -> ( B-A ) -> ( A-B ) -> ( B-A ) etc. 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! Link to comment Share on other sites More sharing options...
jevisan Posted February 14, 2018 Author Share Posted February 14, 2018 alright, this info is really well documented, thanks for your response. I'll test this out and then post my results. Link to comment Share on other sites More sharing options...
samme Posted February 14, 2018 Share Posted February 14, 2018 Probably you can create one Sound object, set it to loop, and then either stop or start/restart it when the player's movement changes. Link to comment Share on other sites More sharing options...
Recommended Posts