[7.小結(jié)]
到這里你已學(xué)完 AS2 的事件處理機(jī)制.現(xiàn)在可以去嘗試下.看看能掌握多少.如果前面你都已掌握,那么學(xué)習(xí) AS3 的 事件處理機(jī)制就相對(duì)會(huì)簡(jiǎn)單點(diǎn).到底有多簡(jiǎn)單?下面給大家了解下 AS3 的事件處理機(jī)制.
示例1.回調(diào)函數(shù): 此示例文檔詳細(xì): Example/AS3/events/CFEvent/CFEventClass.as Example/AS3/events/CFEvent/CFEventExample.as Example/AS3/events/CFEvent/CFEventExample.xml Example/AS3/events/CFEvent/CFeventExample.fla
主類: 打開 CFEventClass.as 文檔,輸入下面的代碼:
package { //---------------------------------------- import flash.net.URLLoader; import flash.net.URLRequest; import flash.events.Event; //---------------------------------------- public class CFEventClass { //---------------------------------------- //定義事件函數(shù). public var complete:Function; //---------------------------------------- public function CFEventClass(url:String) { var loader:URLLoader = new URLLoader(new URLRequest(url)); loader.addEventListener(Event.COMPLETE, this.completeHandler); } public function toString():String { return "[CFEventClass]"; } private function completeHandler(evt:Event):void { //執(zhí)行事件函數(shù). this.complete(evt); } //---------------------------------------- } } 保存文檔.
示例類: 打開 CFEventExample.as 文檔,輸入下面的代碼:
package { //---------------------------------------- import flash.display.Sprite; //---------------------------------------- import CFEventClass; //---------------------------------------- public class CFEventExample extends Sprite { public function CFEventExample() { var ee:CFEventClass = new CFEventClass("CFEventExample.xml"); ee.complete = this.complete; } private function complete(evt:Object):void { trace(evt); } } } 保存文檔.
要加載的 XML 文檔: 打開 CFEventExample.xml 文檔,隨便輸入一些內(nèi)容便可.測(cè)試用.
示例 fla 文檔: 打開 CFEventExample.fla 文檔.在 document class 處輸入 CFEventExample . 如圖:
保存文檔.
測(cè)試 Flash 文檔.在 XML 文檔成功加載后會(huì)在輸出面板中顯示以下內(nèi)容:
[Event type="complete" bubbles=false cancelable=false eventPhase=2] 示例2.使用 EventDispatcher 類: 此示例文檔詳細(xì): Example/AS3/events/Event/EventClass.as Example/AS3/events/Event/EventExample.as Example/AS3/events/Event/EventExample.xml Example/AS3/events/Event/EventExample.fla
主類: 打開 EventClass.as 文檔.輸入下面的代碼:
package { //---------------------------------------- import flash.net.URLLoader; import flash.net.URLRequest; import flash.events.Event; import flash.events.IOErrorEvent; import flash.events.HTTPStatusEvent; import flash.events.EventDispatcher; //---------------------------------------- public class EventClass extends EventDispatcher { //---------------------------------------- public function EventClass(url:String) { var loader:URLLoader = new URLLoader(new URLRequest(url)); loader.addEventListener(Event.COMPLETE, this.completeHandler); loader.addEventListener(IOErrorEvent.IO_ERROR, this.ioErrorHandler); loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, this.httpStatusHandler); } public override function toString():String { return "[EventClass]"; } private function completeHandler(evt:Event):void { this.dispatchEvent(new Event(Event.COMPLETE)); } private function ioErrorHandler(evt:IOErrorEvent):void { this.dispatchEvent(new IOErrorEvent(evt.type, evt.bubbles, evt.cancelable, evt.text)); } private function httpStatusHandler(evt:HTTPStatusEvent):void { this.dispatchEvent(new HTTPStatusEvent(HTTPStatusEvent.HTTP_STATUS, evt.bubbles, evt.cancelable, evt.status)); } //---------------------------------------- } } 保存文檔.
示例類: 打開 EventExample.as 文檔.輸入下面的代碼:
package { //---------------------------------------- import flash.events.Event; import flash.events.IOErrorEvent; import flash.events.HTTPStatusEvent; import flash.display.Sprite; //---------------------------------------- import EventClass; //---------------------------------------- public class EventExample extends Sprite { public function EventExample() { var ee:EventClass = new EventClass("http://localhost/Example/AS3/events/Event/EventExample.xml"); ee.addEventListener(Event.COMPLETE, this.completeHandler); ee.addEventListener(IOErrorEvent.IO_ERROR, this.ioErrorHandler); ee.addEventListener(HTTPStatusEvent.HTTP_STATUS, this.httpStatusHandler); } private function completeHandler(evt:Event):void { trace(evt); } private function ioErrorHandler(evt:IOErrorEvent):void { trace(evt); } private function httpStatusHandler(evt:HTTPStatusEvent):void { trace(evt); } } } 保存文檔.
要加載的 XML 文檔: 打開 EventExample.xml 文檔,隨便輸入一些內(nèi)容便可.測(cè)試用.
示例 fla 文檔: 打開 EventExample.fla 文檔.在 document class 處輸入 EventExample .保存文檔.
測(cè)試 Flash 文檔.在 XML 文檔成功加載后會(huì)在輸出面板中顯示以下內(nèi)容:
[HTTPStatusEvent type="httpStatus" bubbles=false cancelable=false eventPhase=2 status=200] [Event type="complete" bubbles=false cancelable=false eventPhase=2] 將 XML 文檔地址改成錯(cuò)誤的.然后再測(cè)試 Flash 文檔.會(huì)輸出下面的內(nèi)容:
Error opening URL 'http://localhost/Example/AS3/events/Event/EventExamples.xml' [HTTPStatusEvent type="httpStatus" bubbles=false cancelable=false eventPhase=2 status=404] [IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2032: 流錯(cuò)誤。 URL: http://localhost/Example/AS3/events/Event/EventExamples.xml"] 前面所講的 EventDispatcher, Event, IOErrorEvent, HTTPStatusEvent, Timer 類,在 AS3 中成了內(nèi)置類, EventDispatcher 沒有 initialize 方法.但增加了一些其它的方法和屬性.Event 等類也增加了一些方法和屬性. 大家也可以看看. 這里就不講什么了.只是語法稍微不同.但基本跟前面講的差不多.
經(jīng)典論壇討論: http://bbs.blueidea.com/thread-2768209-1-1.html
本文鏈接:http://m.95time.cn/tech/multimedia/2007/4832.asp
出處:藍(lán)色理想
責(zé)任編輯:藍(lán)色月光
上一頁 [AS2]事件處理機(jī)制 -- 建立強(qiáng)大的事件處理機(jī)制 下一頁
◎進(jìn)入論壇Flash專欄版塊參加討論
|