Copy link to clipboard
Copied
Hi,
I have been working on an flash music player and am using autoscript3. I have got the mouse clicks playing and stopping the songs fine, but have run into a problem when I try to assign keyboard events to play and stop multiple songs at once. What's happening is that on the first keyboard event it'll play the song and I hit it again it'll turn off the song (good that's what I want. The problem is though, if I hit the first keyboard event `(192) and then hit the second keyboard event q(81), then try to turn off the first keyboard event '(192) it actually ends up playing mySound2 again instead of turning it off. I believe this is because the keyboard events are on the stage and not individual buttons, like the MouseEvents are. Could someone help me fix the code below:
//mouse events
//button 2
var played2:Boolean=false;
var mySound2:Sound = new DontSpeak();
var myChannel2:SoundChannel = new SoundChannel();
BlueButton2.addEventListener(MouseEvent.CLICK,togglePlay2);
function togglePlay2(event:MouseEvent):void
{
(!played2)?played2 = true : played2 = false;
(played2)?myChannel2 = mySound2.play(0,100) : myChannel2.stop();
(played2)?BlueButton2.gotoAndStop(2) : BlueButton2.gotoAndStop(1)
myChannel2.addEventListener(Event.SOUND_COMPLETE,soundCompleted2);
}
function soundCompleted2(event:Event):void {
played2 = false;
BlueButton2.gotoAndStop(1);
}
//button 3
var played3:Boolean=false;
var mySound3:Sound = new Chances();
var myChannel3:SoundChannel = new SoundChannel();
BlueButton3.addEventListener(MouseEvent.CLICK,togglePlay3);
function togglePlay3(event:MouseEvent):void
{
(!played3)?played3 = true : played3 = false;
(played3)?myChannel3 = mySound3.play() : myChannel3.stop();
(played3)?BlueButton3.gotoAndStop(2) : BlueButton3.gotoAndStop(1)
myChannel3.addEventListener(Event.SOUND_COMPLETE,soundCompleted3);
}
function soundCompleted3(event:Event):void {
played3 = false;
BlueButton3.gotoAndStop(1);
}
//Keyboard events
stage.addEventListener(KeyboardEvent.KEY_DOWN,keydown);
function keydown(event:KeyboardEvent):void
{
if (event.keyCode==192)
(!played2)?played2 = true : played2 = false;
(played2)?myChannel2 = mySound2.play(0,100) : myChannel2.stop();
(played2)?BlueButton2.gotoAndStop(2) : BlueButton2.gotoAndStop(1)
if (event.keyCode==81)
(!played3)?played3 = true : played3 = false;
(played3)?myChannel3 = mySound3.play() : myChannel3.stop();
(played3)?BlueButton3.gotoAndStop(2) : BlueButton3.gotoAndStop(1)
}
Copy link to clipboard
Copied
use some curly brackets in your if-statements so you can see and understand what you're doing.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now