使用按钮单击调用actionscript 3.0函数

时间:2013-02-15 14:37:08

标签: actionscript-3 flash

我使用按钮创建一个简单的flash播放列表,在我的舞台上我有4个按钮,它们是song1,song2,stop和play的按钮。我有一个工作代码,但我决定修改它,因为我以前的代码就像,每首歌他们有每个停止和播放按钮,所以我创建这个有一个动态停止和播放,我创建了一个函数每首歌曲,该功能将改变要加载的歌曲的文件名,

接下来,我首先选择一首歌,(歌曲1或歌曲2)然后我点击停止,然后当我选择一首新歌时出现此错误

错误:错误#2037:以错误顺序调用的函数或早先的调用未成功。     在flash.media::Sound/_load()     在flash.media::Sound/load()     at playlist_fla :: MainTimeline / songSelect1()

我认为它没有调用第二个函数,因为我无法看到我放入其中的痕迹,无论如何是我的代码,对不起长篇文章,

感谢提前

var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var myTransform = new SoundTransform();
var lastPosition:Number = 0;
var song;

song1.addEventListener(MouseEvent.CLICK,songSelect1);
function songSelect1(e:MouseEvent):void{
    song = "<filenameofthe1stsong>";
    mySound.load(new URLRequest(song));
    myTransform.volume = 0.5;
    myChannel.soundTransform = myTransform;
    lastPosition=0;
    trace(1);
}

song2.addEventListener(MouseEvent.CLICK,songSelect2);
function songSelect2(e:MouseEvent):void{
    song = "<filenameofthe2ndsong>";
    mySound.load(new URLRequest(song));
    myTransform.volume = 0.5;
    myChannel.soundTransform = myTransform;
    lastPosition=0;
    trace(2);
}

btnStop.addEventListener(MouseEvent.CLICK,onClickStop);
function onClickStop(e:MouseEvent):void{
    lastPosition = myChannel.position;
    myChannel.stop();
}

btnPlay.addEventListener(MouseEvent.CLICK,onClickPlay);
function onClickPlay(e:MouseEvent):void{
    myChannel = mySound.play(lastPosition);
    myChannel.soundTransform = myTransform();
}

1 个答案:

答案 0 :(得分:0)

<强>校正:

According to Adobe:

  

在Sound对象上调用load()后,您无法在以后加载   将不同的声音文件放入Sound对象中。加载不同的声音   文件,创建一个新的Sound对象。

因此,创建一个新的声音对象将是修复它的唯一方法。

- - - 原帖 - - -

如果在闪存设置中启用调试,则更容易确定导致问题的确切行。也就是说,除了定义声音变换两次之外,您的代码看起来并不正确。试试这个:

var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var myTransform = new SoundTransform();
myChannel.soundTransform = myTransform;

var lastPosition:Number = 0;
var song;

song1.addEventListener(MouseEvent.CLICK,songHandler);
song2.addEventListener(MouseEvent.CLICK,songHandler);
btnStop.addEventListener(MouseEvent.CLICK,onClickStop);
btnPlay.addEventListener(MouseEvent.CLICK,onClickPlay);

function songHandler(e:MouseEvent):void {
    switch (e.currentTarget.name) {
        case "song1":
            songSelect("<filenameofthe1stsong>")
            break;
        case "song2":
            songSelect("<filenameofthe2ndsong>")
            break;
    }
}

function songSelect(songPath:String):void {
    mySound.load(new URLRequest(songPath));
    myChannel.soundTransform.volume = 0.5;
    lastPosition = 0;
    trace("Loading " + songPath);
}

function onClickStop(e:MouseEvent):void {
    lastPosition = myChannel.position;
    myChannel.stop();
}

function onClickPlay(e:MouseEvent):void {
    myChannel = mySound.play(lastPosition);
    myChannel.soundTransform = myTransform();
}