views:

21

answers:

2

Hi, I'm trying to set up a .flv file that will have buttons for two different pieces of sound. I've been able to get the first piece to load and to play and stop, but when I try to load the second piece, I trip up. I'm still a novice, but do think I understand the basic problem -- Actionscript can't distinguish between one sound request and another. The question is, how to fix this and get actionscript to understand that I'm trying to load and play two different sounds associated with two different buttons? I get error codes such as "1151: A conflict exists with definition s in namespace internal." , "duplicate function" problems and "duplicate variable" warnings. I've seen some discussion on this site of a recyclable sound object. I think I'm asking a similar question, but although the person who posed that question also posted a solution, I haven't been able to make it work. Here's the code:

var s:Sound = new Sound();
s.load(new URLRequest("http://www.website.com/Water.mp3"));

wellsplay_btn.addEventListener(MouseEvent.CLICK,playSound);
wellsstop_btn.addEventListener(MouseEvent.CLICK,stopSound);

function playSound(e:MouseEvent):void
{
sc = s.play();
}

function stopSound(e:MouseEvent):void
{
sc.stop();
}

var s:Sound = new Sound();
 s.load(new URLRequest("http://www.website.com/Wells.mp3"));

wellsplay_btn.addEventListener(MouseEvent.CLICK,playSound);
wellsstop_btn.addEventListener(MouseEvent.CLICK,stopSound);

function playSound(e:MouseEvent):void
{
sc = s.play();
}

function stopSound(e:MouseEvent):void
{
sc.stop();
}

The rest of the commands work fine. It's only this section that I'm having trouble with. Thanks in advance for any suggestions.

A: 

This is because you are declaring the variable var s:Sound twice, use different names for them.

Tyler Egeto
Yes, I tried that: var sc:SoundChannel = new SoundChannel(); var s:Sound1 = new Sound(); s.load(new URLRequest("http://www.website/Waves.mp3"));wavesplay_btn.addEventListener(MouseEvent.CLICK,playSound1);wavesstop_btn.addEventListener(MouseEvent.CLICK,stopSound1);function playSound1(e:MouseEvent):void{ sc = s.play();}function stopSound1(e:MouseEvent):void{ sc.stop();}
Chris
But it didn't work. Am I still doing something wrong?
Chris
var s1:Sound = new Sound(); var s2:Sound = new Sound(); should be ok
www0z0k
+1  A: 

Following on the comment you left for Tyler Egeto, giving a new name to a Sound instance means doing the following

 //The variables names are s1 & s2
 var s1:Sound = new Sound();
 var s2:Sound = new Sound();

The first "Sound" , after your variable name is your class name. AS3 is a strongly typed language meaning that you need to state the type of the variable you are declaring. In this case , it's an instance of the Sound class.

In order to play ( or stop ) a specific Sound instance, you will need to identify which button was clicked and react accordingly

 function playSound(e:MouseEvent):void
 {
     switch( e.currentTarget )
     {
          case wellsplay_btn:
           sc = s1.play();
           break;

          case waterplay_btn:
           sc = s2.play();
           break;
     }

 }
PatrickS
I've been beating my head against Actionscript for some time. This is lucid. Thank you.
Chris
I often recommend Colin Mook's Lost Actionscript WeekEnd video series, on AdobeTV. Very laidback intro to AS3 but also very complete overview.
PatrickS