嗨,我是as3的新手,我不确定我是否正确写作。
我有10个声音变量链接到mp3。 我也有一首歌。 我有10个按钮将mp3推入阵列 并且播放按钮只是跟踪songArray,但我不知道如何继续并且能够按照他们输入阵列的相同顺序播放mp3。
package {
import C1.mp3;
import C2.mp3;
import C3.mp3;
import C4.mp3;
import C5.mp3;
import C6.mp3;
import C7.mp3;
import C8.mp3;
import C9.mp3;
import C10.mp3;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.MouseEvent;
import flash.media.Sound;
public class Main extends Sprite {
private var master:Master = new Master();
private var sound2:Sound = new C2.mp3();
private var sound3:Sound = new C3.mp3();
private var sound4:Sound = new C4.mp3();
private var sound5:Sound = new C5.mp3();
private var sound6:Sound = new C6.mp3();
private var sound7:Sound = new C7.mp3();
private var sound8:Sound = new C8.mp3();
private var sound9:Sound = new C9.mp3();
private var sound10:Sound = new C10.mp3();
private var soundArray:Array = [];
public function Main() {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
init();
}
private function init():void {
addChild(master);
master.btn1.btnText.text = "A";
master.btn2.btnText.text = "2";
master.btn3.btnText.text = "3";
master.btn4.btnText.text = "4";
master.btn5.btnText.text = "5";
master.btn6.btnText.text = "6";
master.btn7.btnText.text = "7";
master.btn8.btnText.text = "8";
master.btn9.btnText.text = "9";
master.btn10.btnText.text = "10";
master.btn1.addEventListener(MouseEvent.CLICK, btnClicked);
master.btn2.addEventListener(MouseEvent.CLICK, btnClicked);
master.btn3.addEventListener(MouseEvent.CLICK, btnClicked);
master.btn4.addEventListener(MouseEvent.CLICK, btnClicked);
master.btn5.addEventListener(MouseEvent.CLICK, btnClicked);
master.btn6.addEventListener(MouseEvent.CLICK, btnClicked);
master.btn7.addEventListener(MouseEvent.CLICK, btnClicked);
master.btn8.addEventListener(MouseEvent.CLICK, btnClicked);
master.btn9.addEventListener(MouseEvent.CLICK, btnClicked);
master.btn10.addEventListener(MouseEvent.CLICK, btnClicked);
master.clearBtn.addEventListener(MouseEvent.CLICK, btnClicked);
master.playBtn.addEventListener(MouseEvent.CLICK, btnClicked);
}
private function btnClicked(event:MouseEvent):void {
switch (event.currentTarget.name){
case "btn1":
trace("1");
//sound1.play();
soundArray.push(sound1);
break;
case "btn2":
trace("2");
//sound2.play();
soundArray.push(sound2);
break;
case "btn3":
trace("3");
soundArray.push(sound3);
break;
case "btn4":
trace("4");
soundArray.push(sound4);
break;
case "btn5":
trace("5");
soundArray.push(sound5);
break;
case "btn6":
trace("6");
soundArray.push(sound6);
break;
case "btn7":
trace("7");
soundArray.push(sound7);
break;
case "btn8":
trace("8");
soundArray.push(sound8);
break;
case "btn9":
trace("9");
soundArray.push(sound9);
break;
case "btn10":
trace("10");
soundArray.push(sound10);
break;
}
}
private function clearHandler():void {
// Clean the soundArray back to empty.
soundArray.splice(0);
}
private function playHandler():void{
trace(soundArray);
}
}
}
答案 0 :(得分:0)
先将声音推送到数组,然后新建一个Dictionary
,新建一个Vector
播放列表。
private var soundArray:Array = [ null, sound1, sound2, sound3, sound4, sound5, sound6, sound7, sound8, sound9, sound10 ];
private var soundDic:Dictionary = new Dictionary;
private var playList:Vector.<Sound> = new Vector.<Sound>;
private static const MAX_BTN_NUM:uint = 10;
使用for loop
初始化。
for(var i:int = 1; i <= MAX_BTN_NUM; i++)
{
master["btn"+i].btnText.text = i+"";
master["btn"+i].addEventListener(MouseEvent.CLICK, btnClicked);
soundDic["btn"+i] = soundArray[i];
}
将key-value
用于btnClicked。
private function btnClicked(event:MouseEvent):void {
playList.push( soundDic[event.currentTarget.name] );
}
为int
clearInterval
private var tidx:int = -1;
逐一播放逻辑
private function playOneByOne( sounds:Vector.<Sound>, idx:uint = 0 ):void{
clearInterval( tidx );
if( idx < sounds.length ){
(sounds[idx] as Sound).play();
tidx = setInterval( playOneByOne, (sounds[idx] as Sound).length, sounds, idx + 1 );
}
}
private function clearHandler( event:MouseEvent ):void {
playList.splice( 0, playList.length );
}
private function playHandler( event:MouseEvent ):void{
playOneByOne( playList );
}