我一直在开发一个flash音乐播放器,并且正在使用autoscript3。我有鼠标点击播放和停止歌曲很好,但遇到了一个问题,当我试图分配键盘事件播放和停止多首歌曲一次。发生的事情是,在第一个键盘事件时,它会播放这首歌,我再次点击它,它就会关闭这首歌(很好,这就是我想要的。问题是,如果我点击第一个键盘事件'(192),然后点击第二个键盘事件q(81),然后尝试关闭第一个键盘事件‘(192),它实际上会再次播放mySound2,而不是关闭它。我相信这是因为键盘事件是在舞台上,而不是像MouseEvents那样是单独的按钮。有人能帮我修复一下下面的代码吗:
//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)
}
发布于 2015-02-04 13:08:15
试试这个:
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)
}
}
因为在你的案例中:
if (event.keyCode==192)
(!played2)?played2 = true : played2 = false;
(played2)?myChannel2 = mySound2.play(0,100) : myChannel2.stop();
(played2)?BlueButton2.gotoAndStop(2) : BlueButton2.gotoAndStop(1)
如果event.keyCode != 192,则只有一行
(!played2)?played2 = true : played2 = false;
不会执行,但会执行其他2行。对于keyCode != 81条件也是如此
https://stackoverflow.com/questions/28308958
复制相似问题