Skip to main content
Participating Frequently
September 27, 2014
Answered

Need help with sound buttons

  • September 27, 2014
  • 1 reply
  • 357 views

I'm learning as3 for the 1st time, trying to make a soundboard. I got the 1st sound to work great, but cant get a 2nd sound to play.

Here is my code, and these are saved as buttons, not movie clips:

//1.

var my_sound:s1 = new s1();

var my_channel:SoundChannel = new SoundChannel();

//2.

btn1.addEventListener(MouseEvent.CLICK, playSound);

stop_btn.addEventListener(MouseEvent.CLICK, stopSound);

//3.

function playSound(event:MouseEvent):void{

my_channel = my_sound.play();

}

//4.

function stopSound(event:MouseEvent):void{

my_channel.stop();

}

how do I get "btn2" button instance to play "s2" sound instance?

This code below gives errors, so I must not be changing the right part:

var my_sound:s2 = new s2();

This topic has been closed for replies.
Correct answer Ned Murphy

You would create a line just like the one you have for btn1.

var my_sound:Sound;

var my_channel:SoundChannel = new SoundChannel();

//2.

btn1.addEventListener(MouseEvent.CLICK, playSound);

btn2.addEventListener(MouseEvent.CLICK, playSound);

stop_btn.addEventListener(MouseEvent.CLICK, stopSound);

//3.

function playSound(event:MouseEvent):void{

     if(event.currentTarget == btn1){

              my_sound = new s1();

     } else if(event.currentTarget == btn2){

              my_sound = new s2();

     }

     my_channel = my_sound.play();

}

1 reply

Ned Murphy
Legend
September 27, 2014

You cannot declare the same variable more than once.  You can reassign a variable though.  See if using just " my_sound = new s2(); " works.

Participating Frequently
September 27, 2014

Thanks, sorry I am new to as3, could you change my code to show how I would assign the "s2" sound to "btn2" instance button?

The code below only plays 1 button, I am not sure how or where to add btn2, btn3, etc, and where to add the "new s2" code you typed above. I have looked at sound tutorials and every 1 seems to only tell you how to make 1 sound button. I am trying to make multiple sound buttons that play different sounds

//1.

var my_sound:s1 = new s1();

var my_channel:SoundChannel = new SoundChannel();

//2.

btn1.addEventListener(MouseEvent.CLICK, playSound);

stop_btn.addEventListener(MouseEvent.CLICK, stopSound);

//3.

function playSound(event:MouseEvent):void{

my_channel = my_sound.play();

}

//4.

function stopSound(event:MouseEvent):void{

my_channel.stop();

}

Ned Murphy
Ned MurphyCorrect answer
Legend
September 27, 2014

You would create a line just like the one you have for btn1.

var my_sound:Sound;

var my_channel:SoundChannel = new SoundChannel();

//2.

btn1.addEventListener(MouseEvent.CLICK, playSound);

btn2.addEventListener(MouseEvent.CLICK, playSound);

stop_btn.addEventListener(MouseEvent.CLICK, stopSound);

//3.

function playSound(event:MouseEvent):void{

     if(event.currentTarget == btn1){

              my_sound = new s1();

     } else if(event.currentTarget == btn2){

              my_sound = new s2();

     }

     my_channel = my_sound.play();

}