首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >使多个键盘事件播放和停止多首歌曲

使多个键盘事件播放和停止多首歌曲
EN

Stack Overflow用户
提问于 2015-02-03 21:19:45
回答 1查看 42关注 0票数 0

我一直在开发一个flash音乐播放器,并且正在使用autoscript3。我有鼠标点击播放和停止歌曲很好,但遇到了一个问题,当我试图分配键盘事件播放和停止多首歌曲一次。发生的事情是,在第一个键盘事件时,它会播放这首歌,我再次点击它,它就会关闭这首歌(很好,这就是我想要的。问题是,如果我点击第一个键盘事件'(192),然后点击第二个键盘事件q(81),然后尝试关闭第一个键盘事件‘(192),它实际上会再次播放mySound2,而不是关闭它。我相信这是因为键盘事件是在舞台上,而不是像MouseEvents那样是单独的按钮。有人能帮我修复一下下面的代码吗:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
//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)
}
EN

回答 1

Stack Overflow用户

发布于 2015-02-04 13:08:15

试试这个:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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)
   }
}

因为在你的案例中:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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,则只有一行

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
(!played2)?played2 = true : played2 = false;

不会执行,但会执行其他2行。对于keyCode != 81条件也是如此

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28308958

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文