このサンプルコードは、4つのタイトル/タイトルボタンと4つのタイトルチャネルがあると仮定して作成しました。タイトルサウンド用のサウンドチャンネルを持っています。それで音を止めたり、一時停止したり、再生したりできます。簡単に言うと、を使用してタイトルサウンドを制御できtitleChannel
ます。
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.MouseEvent;
var titleSound1:Sound = new TitleSound1();
var titleSound2:Sound = new TitleSound2();
var titleSound3:Sound = new TitleSound3();
var titleSound4:Sound = new TitleSound4();
var mySound:Sound = new heyjude();
var titleChannel:SoundChannel;
var myChannel:SoundChannel;
var lastPosition:Number = 0;
titleBtn1.addEventListener(MouseEvent.CLICK, title1Selected);
titleBtn1.addEventListener(MouseEvent.CLICK, title2Selected);
titleBtn1.addEventListener(MouseEvent.CLICK, title3Selected);
titleBtn1.addEventListener(MouseEvent.CLICK, title4Selected);
play_btn.addEventListener(MouseEvent.CLICK, onClickPlay);
pause_btn.addEventListener(MouseEvent.CLICK, onClickPause);
function title1Selected(e:MouseEvent){
if(titleChannel != null) titleChannel.stop();
titleChannel = titleSound1.play();
}
function title2Selected(e:MouseEvent){
if(titleChannel != null) titleChannel.stop();
titleChannel = titleSound2.play();
}
function title3Selected(e:MouseEvent){
if(titleChannel != null) titleChannel.stop();
titleChannel = titleSound3.play();
}
function title4Selected(e:MouseEvent){
if(titleChannel != null) titleChannel.stop();
titleChannel = titleSound4.play();
}
function onClickPause(e:MouseEvent):void{
lastPosition = myChannel.position;
myChannel.stop();
}
function onClickPlay(e:MouseEvent):void{
myChannel.stop()
myChannel = mySound.play(lastPosition);
}
他のサウンドと同じようにタイトルサウンドの再生を一時停止して再開する場合はmyChannel
、関数onClickPause
とで行ったのと同じようにしますonClickPlay
。
注:イベントのサウンドを再生する場合(バックグラウンドやブラスト中など)、そのイベントが頻繁に発生する場合は、そのサウンドのSoundChannel
インスタンスを用意して、そのサウンドを制御することを躊躇しないでください。