Copy link to clipboard
Copied
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();
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_c
...Copy link to clipboard
Copied
You cannot declare the same variable more than once. You can reassign a variable though. See if using just " my_sound = new s2(); " works.
Copy link to clipboard
Copied
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();
}
Copy link to clipboard
Copied
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();
}
Copy link to clipboard
Copied
wow thank you, that worked and helped so much!
Like I was saying, every sound tutorial I had looked at was just for making 1 sound button only, none of them talked about changing var for multiple sounds, thanks!
Copy link to clipboard
Copied
You're welcome
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more