2015-12-22 124 views
0

我有一个声音播放每当我翻转一个对象。为了减少恼人的声音,我希望声音只在未播放时播放。由于我需要一些不同的声音,所以我不想使用计时器(如果不是绝对必要的话)。我发现这个:AS3简单的方法来检查声音是否完成

var channel:SoundChannel = snd.play(); 
channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete); 

public function onPlaybackComplete(event:Event) 
{ 
    trace("The sound has finished playing."); 
} 

但我不知道我可以使用它,因为我也有背景音乐。有小费吗?

+0

如果您需要管理多种声音,那么可以考虑采用像声音管理器这样的集中式方法 - 开启/关闭全局声音,控制多种声音/相同声音等等。其中有很多是https:/ /github.com/treefortress/SoundAS,http://evolve.reintroducing.com/2011/01/06/as3/as3-soundmanager-v1-4/,google _as3 sound mananer_ for more – fsbmain

回答

0

你可以选择性地使用这种技巧。不过,如果你打算有很多这样的对象在鼠标悬停的时候触发声音,你可能会决定拥有一个管理类或数据表,每个这样的对象都有一个条目,并且它的字段在声音被播放,并且如此分配的听众将清除该字段中的值,从注册的SoundChannel s中导出正确的条目。预制音响经理会做,但你最好做一个定制的。举个例子:

public class SoundManager { 
    private var _fhOPO:Dictionary; 
    private var _bhOPO:Dictionary; 
    // going sophisticated. Names stand for "forward hash once per object" and "backward" 
    // forward hash stores links to SoundChannel object, backward stores link to object from 
    // a SoundChannel object. 
    // initialization code skipped 
    public static function psOPO(snd:Sound,ob:Object):void 
    { 
     // platy sound once per object 
     if (_fhOPO[ob]) return; // sound is being played 
     var sc:SoundChannel=snd.play(); 
     _fhOPO[ob]=sc; 
     _bhOPO[sc]=ob; 
     sc.addEventListener(Event.SOUND_COMPLETE, _cleanup); 
    } 
    private static function _cleanup(e:Event):void 
    { 
     var sc:SoundChannel=event.target as SoundChannel; 
     if (!sc) return; // error handling 
     var ob:Object=_bhOPO[sc]; 
     _bhOPO[sc]=null; 
     _fhOPO[ob]=null; // clean hashes off now obsolete references 
     sc.removeEventListener(Event.SOUND_COMPLETE, _cleanup); 
     // and clean the listener to let GC collect the SoundChannel object 
    } 
} 

然后,当你需要播放声音,但限制每个对象的频道的一个实例,你叫SoundManager.psOPO(theSound,this);提供一个按钮参考,而不是this如果需要的话。如果需要,您可以使用普通Sound.play()以及此类声音管理,或者在需要时使用其他类型的BGM管理或其他类型的声音。

相关问题