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;
        }
    }

我正在尝试循环播放嵌入的音乐,但只播放第一首随机歌曲,然后只是静音......不明白为什么下一首歌曲不播放,谁能告诉我?

4

1 回答 1

0

在您的complete处理程序的条件中,您正在测试music.Length(注意大写 L),它将在执行时立即引发错误。您还需要修复当前允许索引增加超出数组范围的测试(请记住,数组元素是 0 索引的)。

此外,由于您没有PlaySongFromIndex从条件中调用该方法,else因此程序不会每 3 次超过第一首歌曲。

尝试使用以下内容更新您的代码:

public function songFinished(e:Event){
    if(currentSongIndex < music.length - 1){
        currentSongIndex++;
    } else {
        currentSongIndex=0;
    }
    PlaySongFromIndex(currentSongIndex);
}
于 2013-05-30T23:47:36.973 回答