Hi all,
I’ve been working on playing sound through AS. But now I have more than one sound that I would like to play (seperately).
So on my stage I have two buttons, two movieclips and in my library I have two audioclips. When I press button A, movieclip A and audioclip A need to play. When I press button B, movieclip B and audioclip B need to play. So far so good. But now I want to pause and continue the MC and sounds. Pause works fine, but when I press play both sounds begin to play (when I’ve pressed both buttons).
To make it somewhat clearer, I’ve added my swf-file (so I tried, but apperantly it is not allowed…). Below is my code.
import flash.display.MovieClip;
import flash.media.Sound;
var snd_screenen:Sound = new SCREENEN();
var snd_behandelen:Sound = new BEHANDELEN();
var mcArray:Array = [mc_screenen, mc_behandelen];
var btnArray:Array = [screenen, behandelen];
var sndArray:Array = [snd_screenen, snd_behandelen];
var clickedDict:Dictionary = new Dictionary(true);
mc_vinkje.visible = false;
btn_play.visible = false;
btn_pauze.visible = false;
for(var i:int = 0; i < mcArray.length; i++)
{
mcArray*.visible = false;
mcArray*.stop();
}
for(var j:int = 0; j < btnArray.length; j++)
{
btnArray[j].visible = true;
btnArray[j].addEventListener(MouseEvent.MOUSE_DOWN, onBTNklik);
clickedDict[btnArray[j]] = false;
}
function onBTNklik (event:MouseEvent):void
{
clickedDict[event.currentTarget] = true;
var lastPosition:Number = 0;
SoundMixer.stopAll();
var currentBTN:String = event.currentTarget.name;
var currentMC:MovieClip = getChildByName ("mc_" + currentBTN) as MovieClip;
var channel:SoundChannel = new SoundChannel();
if (currentMC == mc_behandelen)
{
var currentSND = snd_behandelen;
}
if (currentMC == mc_screenen)
{
var currentSND = snd_screenen;
}
var len:uint = Math.min(mcArray.length, btnArray.length, sndArray.length);
for(var k:int = 0; k < len; k++)
{
mcArray[k].visible = false;
mcArray[k].gotoAndStop(0);
}
if (mcArray[k] == btnArray[k] && btnArray[k] == sndArray[k])
{
btn_play.addEventListener(MouseEvent.MOUSE_DOWN, onPlayklik);
btn_pauze.addEventListener(MouseEvent.MOUSE_DOWN, onPauzeklik);
btn_pauze.visible = true;
btn_play.visible = false;
currentMC.visible = true;
currentMC.play();
channel = currentSND.play(0);
function onPlayklik (event:MouseEvent):void
{
for(var k:int = 0; k < len; k++)
mcArray[k].stop();
btn_pauze.visible = true;
btn_play.visible = false;
currentMC.play();
channel = currentSND.play(lastPosition);
}
function onPauzeklik (event:MouseEvent):void
{
btn_play.visible = true;
btn_pauze.visible = false;
currentMC.stop();
lastPosition = channel.position;
channel.stop();
}
}
mc_vinkje.visible = check();
}
function check():Boolean
{
var isValid:Boolean = true;
var length:int = 0;
for (var key:Object in clickedDict)
{
length++; // check direct hoeveel keys er in de dictionary zitten
isValid &&= (clickedDict[key] == true); // de value zou ook false kunnen zijn
}
// check voor de zekerheid of het aantal geklikte knoppen gelijk is aan het totaal aantal knoppen
isValid &&= (btnArray.length == length);
return isValid;
}
So I need something in my code to define the ‘currentSND’. First I tried that with getChildByName (the same way as I define my ‘currentMC’). But that didn’t work (I got the error that something was null), so I guess you can get the name of a sound that way