Copy link to clipboard
Copied
Hi guys,
How to set different background sound if I have 3 labels.
For example, I have 3 settings like : Market, School, and Beach.
Everytime user change their option, we move to different label using gotoAndPlay("beach") for example.
Then, how to change the background music right after they choose another place?
I've found some script like:
Script |
---|
var snd:Sound = new SoundPlay(); channel1 = snd.play(); |
But I'm not sure how to use it in different labels.
Thank you.
Copy link to clipboard
Copied
I hope it helps!
Code:
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.net.URLRequest;
var sChannel:SoundChannel;
var bgmList:Vector.<String> = new <String>
[
"bgm/369711__mrguff__hit-impact.mp3",
"bgm/146718__fins__button.mp3",
"bgm/264446__kickhat__open-button-1.mp3"
];
function playSound(path:String, volume:Number = 1, start:Number = 0, loops:int = 0):void
{
var sound:Sound = new Sound();
var sTransform:SoundTransform = new SoundTransform();
if (sChannel) sChannel.stop();
sound.load(new URLRequest(path));
sChannel = sound.play(start, loops);
sTransform.volume = volume;
sChannel.soundTransform = sTransform;
}
function onMouse(e:MouseEvent):void
{
if (e.type == MouseEvent.CLICK)
{
if (e.currentTarget == button1)
{
playSound(bgmList[0], 1, 0, 100);
gotoAndPlay("bgm1");
}
else if (e.currentTarget == button2)
{
playSound(bgmList[1], 1, 0, 100);
gotoAndPlay("bgm2");
}
else if (e.currentTarget == button3)
{
playSound(bgmList[2], 1, 0, 100);
gotoAndPlay("bgm3");
}
}
}
button1.addEventListener(MouseEvent.CLICK, onMouse);
button2.addEventListener(MouseEvent.CLICK, onMouse);
button3.addEventListener(MouseEvent.CLICK, onMouse);
stop();
Find more inspiration, events, and resources on the new Adobe Community
Explore Now