[4.使用 addListener 方法(AsBroadcaster / BroadcasterMX 類)]
在 MovieClipLoader 和 Tween 等類中都用到了 addListener 方法來(lái)注冊(cè)偵聽(tīng). 我們?cè)撛趺从媚? Macromedia 為我們提供了如下二個(gè)類: AsBroadcaster 屬于系統(tǒng)類. BroadcasterMX 屬于自定義類.類路徑: mx.transitions.BroadcasterMX 這兩個(gè)類的很相似.它們都是提供事件通知和偵聽(tīng)器管理功能. 但 AsBroadcaster 類是系統(tǒng)內(nèi)置的.執(zhí)行效率會(huì)比 BroadcasterMX 類快很多.所以這里我們就用 AsBroadcaster 類來(lái)講解. 此類詳細(xì): public addListener(listenerObj:Object):Boolean ---注冊(cè)偵聽(tīng)器. public broadcastMessage(eventName:String):Void ---廣播消息/發(fā)送事件. public static initialize(obj:Object):Void ---將事件通知和偵聽(tīng)器管理功能添加到給指定的對(duì)象 public removeListener(listenerObj:Object):Boolean ---移除偵聽(tīng)器. public _listeners:Array [只讀] ---對(duì)已注冊(cè)偵聽(tīng)器對(duì)象的引用.
該怎么用呢? 看下面的示例: [4.1.示例: AsBEventClass 類(簡(jiǎn)單的示例)] 此示例文檔詳細(xì): Example/AS2/events/AsBEvent/AsBEventClass.as Example/AS2/events/AsBEvent/AsBEventExample.as Example/AS2/events/AsBEvent/AsBEventExample.xml Example/AS2/events/AsBEvent/AsBEventExample.fla
主類: 打開(kāi) CFEventClass.as 文檔,輸入下面的代碼:
import mx.utils.Delegate; //---------------------------------------- class AsBEventClass extends AsBroadcaster { //---------------------------------------- public var tXML:XML; //---------------------------------------- public function AsBEventClass(url:String) { AsBroadcaster.initialize(AsBEventClass.prototype); this.tXML = new XML(); this.tXML.onLoad = Delegate.create(this, this.XMLonLoad); this.tXML.load(url); } private function XMLonLoad(success:Boolean):Void { if (success) { this.broadcastMessage("complete"); } } //---------------------------------------- } 保存文檔. 這里首先繼承 AsBroadcaster 類.然后在構(gòu)造函數(shù)中使用
AsBroadcaster.initialize(AsBEventClass.prototype); 來(lái)獲取事件通知和偵聽(tīng)器管理功能.最后使用
this.broadcastMessage("complete"); 來(lái)廣播消息或發(fā)送事件. 當(dāng)然你也可以選擇不繼承它,但需要在類中添加下面的方法:
public var addListener:Function; public var removeListener:Function; private var broadcastMessage:Function; 示例類: 打開(kāi) CFTimerExample.as 文檔.輸入下面的代碼:
import AsBEventClass; //---------------------------------------- class AsBEventExample { public function AsBEventExample() { var ee:AsBEventClass = new AsBEventClass("AsBEventExample.xml"); ee.addListener(this); } private function complete():Void { trace("complete"); } } 保存文檔. 在示例類中我們創(chuàng)建主類的一個(gè)實(shí)例.來(lái)加載 AsBEventExample.xml 文檔.然后添加偵聽(tīng)器. 如果要移除偵聽(tīng)器,請(qǐng)使用下面的方法:
ee.removeListener(this); 要加載的 XML 文檔: 打開(kāi) AsBEventExample.xml 文檔,隨便輸入一些內(nèi)容便可.測(cè)試用.
示例 fla 文檔: 打開(kāi) AsBEventExample.fla 文檔.將"圖層 1"重命名為 Actions.并在第一幀中輸入下面的代碼:
new AsBEventExample(); 保存文檔. 測(cè)試 Flash 文檔.在 XML 文檔成功加載后會(huì)在輸出面板中顯示以下內(nèi)容
complete 如需給事件傳遞參數(shù),在發(fā)送事件時(shí)如下添加參數(shù):
this.broadcastMessage("complete", 參數(shù)1, 參數(shù)2, 參數(shù)N); 在調(diào)用事件時(shí):
private function complete(參數(shù)1, 參數(shù)2, 參數(shù)N) { trace([參數(shù)1, 參數(shù)2, 參數(shù)N]); } 下面再看一個(gè)示例: [4.2.示例: AsBTimer 類(定時(shí)器)] 此示例文檔詳細(xì): Classes/AS2/utils/AsBTimer.as Example/AS2/utils/AsBTimer/AsBTimerExample.as Example/AS2/utils/AsBTimer/AsBTimerExample.fla
主類: 打開(kāi) AsBTimer.as 文檔,輸入下面的代碼:
import AS2.utils.CFDelegate; //---------------------------------------- class AS2.utils.AsBTimer extends AsBroadcaster { //---------------------------------------- private var _timerID:Number; private var _delay:Number; private var _repeatCount:Number; private var _currentCount:Number = 0; private var _running:Boolean; //---------------------------------------- /* @parameter delay: 延遲,單位毫秒. @parameter repeatCount: 重復(fù)次數(shù).默認(rèn)為Infinity(正無(wú)窮大); */ public function AsBTimer(delay:Number, repeatCount:Number) { AsBroadcaster.initialize(AsBTimer.prototype); if (isNaN(delay)) { return; } if (isNaN(repeatCount)) { repeatCount = Infinity; } this._delay = delay; this._repeatCount = repeatCount; } //---------------------------------------- public function reset():Void { this._currentCount = 0; this.stop(); } public function start():Void { this._timerID = setInterval(CFDelegate.create(this, this.startTimer), this._delay); this._running = true; } public function stop():Void { clearInterval(this._timerID); this._running = false; } public function toString():String { return "[AsBTimer]"; } //---------------------------------------- private function startTimer():Void { this._currentCount++; this.broadcastMessage("timer", this._currentCount); if (this._currentCount == this._repeatCount) { this.reset(); this.broadcastMessage("timerComplete"); } } //---------------------------------------- public function get delay():Number { return this._delay; } public function set delay(d:Number):Void { this._delay = d; } public function get repeatCount():Number { return this._repeatCount; } public function set repeatCount(r:Number):Void { this._repeatCount = r; } public function get currentCount():Number { return this._currentCount; } public function get running():Boolean { return this._running; } //---------------------------------------- } 因此類詳細(xì)前面已講,功能一樣,只是事件處理機(jī)制不同而已.這里就不再啰嗦.
示例類: 打開(kāi) AsBTimerExample.as 文檔.輸入下面的代碼:
import AS2.utils.AsBTimer; //---------------------------------------- class AsBTimerExample { //---------------------------------------- public function AsBTimerExample() { var te:AsBTimer = new AsBTimer(1000, 5); te.addListener(this); te.start(); } private function timer(currentCount:Number):Void { trace("timer: " + currentCount); } private function timerComplete():Void { trace("timerComplete: "); } //---------------------------------------- } 保存文檔.
示例 fla 文檔: 打開(kāi) AsBTimerExample.fla 文檔,將"圖層 1" 重命名為 Actions.并在第一幀中輸入下面的代碼:
new AsBTimerExample(); 測(cè)試 Flash 文檔.輸出面板會(huì)陸續(xù)顯示以下的內(nèi)容:
timer: 1 timer: 2 timer: 3 timer: 4 timer: 5 timerComplete: 下一節(jié)講:[5.使用 addEventListener 方法(EventDispatcher 類)]
經(jīng)典論壇討論: http://bbs.blueidea.com/thread-2768209-1-1.html
出處:藍(lán)色理想
責(zé)任編輯:藍(lán)色月光
上一頁(yè) [AS2]事件處理機(jī)制 -- 使用回調(diào)函數(shù) 下一頁(yè) [AS2]事件處理機(jī)制 -- 使用addListener方法 二
◎進(jìn)入論壇Flash專欄版塊參加討論
|