Hi there,
I’m very pleased to have completed a small Flash project where I have buttons controlling a sound (and also a movieclip with a needle moving over an image of the sound data).
I’ve seen examples/tutorials in which the AS3 code is much more complicated, especially with removing event listeners then re-adding.
When the sound is complete, I tell the play button to show & the other buttons to hide, so I have the same EventListener added to the functions for the play, continue & restart buttons.
Should I be removing the SOUND_COMPLETE EventListener from the pause button, or doesn’t it matter? It already seems to work perfectly, but I’d like to learn the best way to use AS3! (I’ve posted the code below.)
Thanks
needleMC.stop();
pauseBtn.visible = false;
restartBtn.visible = false;
continueBtn.visible = false;
needleMC.visible = false;
var mySound:Sound= new treefrog();
var myChannel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0;
playBtn.addEventListener(MouseEvent.CLICK, playHandler);
pauseBtn.addEventListener(MouseEvent.CLICK, pauseHandler);
restartBtn.addEventListener(MouseEvent.CLICK, restartHandler);
continueBtn.addEventListener(MouseEvent.CLICK, continueHandler);
function playHandler (e:MouseEvent):void {
myChannel = mySound.play();
needleMC.play();
pauseBtn.visible = true;
restartBtn.visible = false;
playBtn.visible = false;
continueBtn.visible = false;
needleMC.visible = true;
myChannel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
}
function pauseHandler (e:MouseEvent):void {
lastPosition = myChannel.position;
myChannel.stop();
needleMC.stop();
pauseBtn.visible = false;
playBtn.visible = false;
restartBtn.visible = true;
continueBtn.visible = true;
needleMC.visible = true;
}
function continueHandler (e:MouseEvent):void {
myChannel = mySound.play(lastPosition);
needleMC.play();
pauseBtn.visible = true;
restartBtn.visible = false;
playBtn.visible = false;
continueBtn.visible = false;
needleMC.visible = true;
myChannel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
}
function restartHandler (e:MouseEvent):void {
myChannel = mySound.play();
needleMC.gotoAndPlay(1);
pauseBtn.visible = true;
restartBtn.visible = false;
playBtn.visible = false;
continueBtn.visible = false;
needleMC.visible = true;
myChannel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
}
function soundCompleteHandler(e:Event):void {
pauseBtn.visible = false;
restartBtn.visible = false;
playBtn.visible = true;
continueBtn.visible = false;
needleMC.visible = false;
}