Skip to main content
tiborf16537063
Participant
January 15, 2023
Answered

createjs.Sound.play random soundfx

  • January 15, 2023
  • 3 replies
  • 442 views

Hi!

 

Anybody knows how to play an event a random sound instead of 1 redifinied sound?

createjs.Sound.play("soundfx");

this is the code works like a charm, but i want 2 more soundfx being randomized and played one at the event.

Thanx a lot!

    This topic has been closed for replies.
    Correct answer JoãoCésar17023019

    Hi.

     

    Try this:

    var linkages = [ "soundfx", "soundfx1", "soundfx2" ];
    
    createjs.Sound.play(linkages[Math.floor(Math.random() * linkages.length)]);

     

    Regards,

    JC

    3 replies

    kglad
    Community Expert
    Community Expert
    January 16, 2023

    (and note for others, you need user interaction, eg a click, before a browser will allow a sound to play.)

    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    January 16, 2023

    Hi.

     

    Try this:

    var linkages = [ "soundfx", "soundfx1", "soundfx2" ];
    
    createjs.Sound.play(linkages[Math.floor(Math.random() * linkages.length)]);

     

    Regards,

    JC

    tiborf16537063
    Participant
    January 18, 2023

    Thanx @JoãoCésar17023019 and @kglad ! The sample code perfectly solved my problem!

    JoãoCésar17023019
    Community Expert
    Community Expert
    January 18, 2023

    You're welcome!

    kglad
    Community Expert
    Community Expert
    January 15, 2023

    put your sounds' linkage id's in an array and select a random element to play.

    tiborf16537063
    Participant
    January 16, 2023

    Thanx @kglad ! You have some sample code for this?