2012-07-02 58 views
0

因此,我有一個soundHandler類,它應該播放聲音,然後在聲音完成播放時指向時間軸上的某個函數。但不知何故,當我嘗試時,只有其中一種聲音播放。 編輯:聲音播放後,沒有任何反應,即使我設置了應該做某事的EventHandler。 下面的代碼:ActionScript 2:事件不會觸發?

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

我在這一個全盤損失。已經浪費了數小時才弄清楚了。 任何幫助將深表謝意。

回答

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