制作多个键盘事件可播放和停止多首歌曲

时间:2015-02-03 21:19:45

标签: flash

我一直在使用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)
}

1 个答案:

答案 0 :(得分:0)

试试这个:

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条件

也是如此