Skip to main content
Participant
October 11, 2012
Question

How to generate a random sound from one button?

  • October 11, 2012
  • 3 replies
  • 1520 views

CS6 Flash, actionscript3

Hello, I want a different sound to be produced each time one button is clicked? Can anyone tell me how to do this or where to find the answer? I'm a beginner to animation. Thanks, Breanna

This topic has been closed for replies.

3 replies

Participating Frequently
October 11, 2012

Also thought i'd mention a couple books I've used to help learn that are really good. The adobe classroom in a book series, one is for actionscript 3.0 and one is for flash.They are great to help learn the basics. Each chapter was just a simple example you make that just take 30mins to little over an hour to do.

Participating Frequently
October 11, 2012

I'm still a beginner myself but this is how i would do it:

first i would load all the sounds. It would look something like this:

var gameSound1:Sound = new Sound();

var myChannel1:SoundChannel = new SoundChannel();

gameSound1.load(new URLRequest("Sound1.mp3"));

var gameSound2:Sound = new Sound();

var myChannel2:SoundChannel = new SoundChannel();

gameSound2.load(new URLRequest("Sound2.mp3"));

then i would make a  mouse click event listener for the button:

button.addEventListener(MouseEvent.CLICK, randomSound);

then the function that randomizes the music and plays it:

function randomSound(myEvent:MouseEvent):void{

    var musicNumber:Number;

    musicNumber= Math.ceil(Math.random()*2);

    if(musicNumber == 1){

        myChannel1 =gamesound1.play(0,999);

    }else if(musicNumber == 2){

       myChannel2 =gamesound2.play(0,999);

    }

}

Ned Murphy
Legend
October 11, 2012

Here's a slightly different approach which uses the same Sound variable each time to create a new Sound object.  You need to be able to stop the current channel in order to start a new one and avoid having things overlapping/piling up on one another. 

The conditional assigns the file to load rather than picking which Sound object to play. 

I also changed the randomizing code slightly.  Math.random returns a vaoue  from 0 to < 1, so you could end up with a zero and your conditions wouldn't cover it.  By going with the floor instead of the ceiling the results will only ever be 0 or 1.

var gameSound:Sound;

var myChannel:SoundChannel = new SoundChannel();;

button.addEventListener(MouseEvent.CLICK, randomSound);

function randomSound(myEvent:MouseEvent):void{

    gameSound = new Sound();

 

    var musicNumber:Number = Math.floor(Math.random()*2);

    myChannel.stop();

    if(musicNumber == 0){

        gameSound.load(new URLRequest("Sound1.mp3"));

    } else if(musicNumber == 1){

        gameSound.load(new URLRequest("Sound2.mp3"));

    }

    myChannel = gameSound.play(0,999);

}

Participating Frequently
October 11, 2012

Thats true i forgot that part on mine. Would have to put a stop the previous play on mine before the play to keep from overlapping.

Ned Murphy
Legend
October 11, 2012

What would you do to generate a specific sound from one button?  Show the code you would use.