2013-05-30 51 views
0
[Embed('sounds/music1.mp3')] 
public var Music1:Class; 

[Embed('sounds/music2.mp3')] 
public var Music2:Class; 

[Embed('sounds/music3.mp3')] 
public var Music3:Class; 

public var music:Array; 
public var currentSongIndex:int; 

    public function complete():void { 

     stage.scaleMode = StageScaleMode.SHOW_ALL; 
     stage.frameRate = 32; 
     music = new Array(); 
     music.push(new Music1()); 
     music.push(new Music2()); 
     music.push(new Music3()); 

     currentSongIndex = Math.floor(Math.random() * music.length); 
     var playFirst:SoundAsset = music[currentSongIndex] as SoundAsset; 
     playFirst.addEventListener(Event.COMPLETE, songFinished); 
     playFirst.play(); 
    } 

    public function PlaySongFromIndex(songIndex:int){ 
     var playFirst:SoundAsset = music[currentSongIndex] as SoundAsset; 
     playFirst.addEventListener(Event.COMPLETE, songFinished); 
     playFirst.play(); 
    } 

    public function songFinished(e:Event){ 
     if(currentSongIndex < music.Length){ 
      currentSongIndex++; 
      PlaySongFromIndex(currentSongIndex); 
     } else { 
      currentSongIndex=0; 
     } 
    } 

我'嘗試循環的嵌入式音樂,但只有第一個隨機歌曲播放,然後是隻有沉默......不明白爲什麼下一首歌曲沒有按不玩,誰能告訴我?不能循環內嵌聲音柔性

回答

0

在您的complete處理程序的條件中,您正在測試music.Length(注意大寫L),它將在執行時立即拋出錯誤。您還需要修復當前允許索引超出數組範圍的測試(請記住數組元素是0索引的)。

此外,由於您不會從else條件調用PlaySongFromIndex方法,所以程序不會以每三次大約一次的速度超過第一首歌曲。

嘗試用更新的代碼如下:

public function songFinished(e:Event){ 
    if(currentSongIndex < music.length - 1){ 
     currentSongIndex++; 
    } else { 
     currentSongIndex=0; 
    } 
    PlaySongFromIndex(currentSongIndex); 
} 
+0

我已經改變了這一點,但第一首歌后,仍是未來不打......想不通爲什麼 –

+0

你查看控制檯中的任何錯誤?嘗試在歌曲完整處理程序中添加跟蹤語句或斷點,以確保正在執行。 –

+0

其實,看到更新的答案。你的條件允許索引超出你的數組的範圍,並且你正在測試音樂。長度而不是music.length,它會在執行時立即拋出錯誤。 –