0

所以我有一个 soundHandler 类,它应该播放声音,然后在声音完成播放后指向时间轴上的一个函数。但不知何故,当我尝试时,只有一种声音会播放。编辑:声音播放后,没有任何反应,即使我设置了应该做某事的 EventHandlers。这是代码:

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");

我完全失去了这个。已经浪费了几个小时来弄清楚。任何帮助将不胜感激。

4

1 回答 1

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();
    }
}
于 2012-07-03T03:12:44.587 回答