我要做的是在鼠标按下事件上循环播放声音,然后在鼠标按下事件上循环播放我希望完成迭代它然后停止循环。
下面这个逻辑应该有用,但我不知道为什么不是。
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.events.Event;
import flash.media.SoundChannel;
//ExplosionSound is .wav file linked in my library.
var s:Sound=new ExplosionSound();
var sc:SoundChannel = new SoundChannel();
stage.addEventListener(MouseEvent.MOUSE_DOWN, loopSound);
stage.addEventListener(MouseEvent.MOUSE_UP, stopLoop);
function loopSound(e:MouseEvent):void {
sc=s.play(0,9999);
}
function stopLoop(e:MouseEvent):void {
sc.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
}
function soundCompleteHandler(e:Event):void{
sc.removeEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
sc.stop();
}
我可以把sc.stop();在我的stopLoop()函数中,但它立即停止了声音。
我需要的是,例如:说你有机关枪射击,当你停止射击时,最后一颗子弹声音不会立即停止,它就会完成。
希望这是有道理的。
感谢您的帮助。
答案 0 :(得分:1)
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.events.Event;
import flash.media.SoundChannel;
//ExplosionSound is .wav file linked in my library.
var s:Sound=new ExplosionSound();
var sc:SoundChannel = new SoundChannel();
var isMouseDown:Boolean = false;
stage.addEventListener(MouseEvent.MOUSE_DOWN, loopSound);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
function loopSound(e:MouseEvent):void {
isMouseDown = true;
playSound();
}
function playSound():void
{
sc=s.play(0);
sc.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
}
function onMouseUpHandler(e:MouseEvent):void {
isMouseDown = false;
}
function soundCompleteHandler(e:Event):void{
sc.removeEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
sc.stop();
if (isMouseDown)
{
playSound();
}
}
只需在mousedown上播放一次声音(但是无论鼠标是否仍然按下,都要保持标记)并添加一个SOUND_COMPLETE处理程序。如果声音结束且鼠标仍然按下,则重放声音。如果释放鼠标按钮,标志将被重置,完成后声音将不会重放.... tataaaaa