ActionScript 2:事件不会触发?

时间:2012-07-02 03:33:38

标签: actionscript-2 audio

所以我有一个soundHandler类,它应该播放声音,然后在声音播放完毕后指向时间轴上的某个功能。但不知何故,当我尝试时,只有一个声音播放。 编辑:声音播放后,即使我设置了应该执行某些操作的EventHandler,也没有任何反应。 这是代码:

import mx.events.EventDispatcher;

class soundHandler {
private var dispatchEvent:Function;
public var addEventListener:Function;
public var removeEventListener:Function;
var soundToPlay;
var soundpath:String;
var soundtype:String;
var prefix:String;
var mcname:String;

public function soundHandler(soundpath:String, prefix:String, soundtype:String, mcname:String) {
    EventDispatcher.initialize(this);
    _root.createEmptyMovieClip(mcname, 1);
    this.soundpath = soundpath;
    this.soundtype = soundtype;
    this.prefix = prefix;
    this.mcname = mcname;
}

function playSound(file, callbackfunc) {
    _root.soundToPlay = new Sound(_root.mcname);
    _global.soundCallbackfunc = callbackfunc;
    _root.soundToPlay.onLoad = function(success:Boolean) {
        if (success) {
            _root.soundToPlay.start();
        }
    };
    _root.soundToPlay.onSoundComplete = function():Void {
        trace("Sound Complete: "+this.soundtype+this.prefix+this.file+".mp3");
        trace(arguments.caller);
        dispatchEvent({type:_global.soundCallbackfunc});
        trace(this.toString());
        trace(this.callbackfunction);
    };
    _root.soundToPlay.loadSound("../sound/"+soundpath+"/"+soundtype+prefix+file+".mp3", true);
    _root.soundToPlay.stop();
}
}

这是.fla文件中的代码:

var playSounds:soundHandler = new soundHandler("signup", "su", "s", "mcs1");
var file = "000";
playSounds.addEventListener("sixtyseconds", this);
playSounds.addEventListener("transition", this);

function sixtyseconds() {
        trace("I am being called! Sixtyseconds");
        var phase = 1;
        var file = random(6);
        if (file == 0) {
            file = 1;
        }
        if (file<10) {
    file = "0"+file;
        }
        file = phase+file;
        playSounds.playSound(file, "transition");
}
function transition() {
trace("this works");
}
playSounds.playSound(file, "sixtyseconds");

我完全失去了这个。一直在浪费时间去弄明白。 任何帮助都将深表感谢。

1 个答案:

答案 0 :(得分:0)

好吧,在使用Delegate类之后,我使用了这段代码:

import mx.events.EventDispatcher;
import mx.utils.Delegate;

class soundHandler{

    public var addEventListener:Function;
    public var removeEventListener:Function;
    private var dispatchEvent:Function;
    private var soundToPlay;
    private var soundpath:String;
    private var soundtype:String;
    private var prefix:String;
    private var mcname:String;

    public function soundHandler(soundpath:String, prefix:String, soundtype:String,    mcname:String) {
        EventDispatcher.initialize(this);
        _root.createEmptyMovieClip(mcname, 1);
        this.soundpath = soundpath;
        this.soundtype = soundtype;
        this.prefix = prefix;
        this.mcname = mcname;
    }

    private function playSoundCallback(file, callbackfunc) {
        var soundname = "s"+file;
        _root[soundname] = new Sound(_parent.mcname);
        _root[soundname].onLoad = function(success:Boolean) {
            if (success) {
                _root[soundname].start();
            }
        };
        trace("Play Sound: "+file+".mp3; Function To Call: "+callbackfunc);
        _root[soundname].onSoundComplete = Delegate.create(this,function():Void {
            trace("Sound Complete: "+this.soundtype+this.prefix+this.file+".mp3");
            dispatchEvent({type:callbackfunc});
        });
        _root[soundname].loadSound("../sound/"+soundpath+"/"+soundtype+prefix+file+".mp3", true);
        _root[soundname].stop();
    }

    private function playRandomSoundCallback(phase, scope, callbackfunc) {
        var file = random(scope);
        if (file == 0) {
            file = 1;
        }
        if (file<10) {
            file = "0"+file;
        }
        file = phase+file;
        playSoundCallback(file, callbackfunc);
    }

    private function playSound(file) {
        var soundname = "s"+file;
        _root[soundname] = new Sound(_root.mcname);
        _root[soundname].onLoad = function(success:Boolean) {
            if (success) {
                _root[soundname].start();
            }
        };
        trace("Play Sound: "+file+".mp3");
        _root[soundname].loadSound("../sound/"+soundpath+"/"+soundtype+prefix+file+".mp3", true);
        _root[soundname].stop();
    }

    private function playSoundRandom(phase, scope) {
        var file = random(scope);
        if (file == 0) {
            file = 1;
        }
        if (file<10) {
            file = "0"+file;
        }
        file = phase+file;
        playSound(file);
    }

    private function playSoundAS(identifier) {
        var soundname = identifier;
        _root[soundname] = new Sound(_root.mcname);;
        trace("Play Sound AS: "+identifier+".mp3");
        _root[soundname].attachSound("identifier");
        _root[soundname].start();
    }
}