Skip to main content
tiborf16537063
Participant
Jan 15, 2023 9:12 AM UTC
Answered

createjs.Sound.play random soundfx

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 is closed to new replies. Start a new post to keep the conversation going.
    JoãoCésar17023019
    JoãoCésar17023019Correct answer
    Community Expert
    Jan 16, 2023 11:44 AM UTC

    Hi.

     

    Try this:

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

     

    Regards,

    JC

    7 replies

    kglad
    Community Expert
    Community Expert
    Jan 16, 2023 3:56 PM UTC

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

    Translate
    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    Jan 16, 2023 11:44 AM UTC

    Hi.

     

    Try this:

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

     

    Regards,

    JC

    Translate
    tiborf16537063
    Participant
    Jan 18, 2023 1:25 PM UTC

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

    Translate
    JoãoCésar17023019
    Community Expert
    Community Expert
    Jan 18, 2023 1:36 PM UTC

    You're welcome!

    Translate
    kglad
    Community Expert
    Community Expert
    Jan 15, 2023 1:50 PM UTC

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

    Translate
    tiborf16537063
    Participant
    Jan 16, 2023 2:31 AM UTC

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

    Translate