Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Need help with sound buttons

New Here ,
Sep 27, 2014 Sep 27, 2014

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();

TOPICS
ActionScript
379
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Sep 27, 2014 Sep 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_c

...
Translate
LEGEND ,
Sep 27, 2014 Sep 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 27, 2014 Sep 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();

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 27, 2014 Sep 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();

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 27, 2014 Sep 27, 2014

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 27, 2014 Sep 27, 2014
LATEST

You're welcome

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines