| [7.小结] 到这里你已学完 AS2 的事件处理机制.现在可以去尝试下.看看能掌握多少.如果前面你都已掌握,那么学习 AS3 的事件处理机制就相对会简单点.到底有多简单?下面给大家了解下 AS3 的事件处理机制.
 示例1.回调函数:此示例文档详细:
 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
 {
 //----------------------------------------
 //定义事件函数.
 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
 {
 //执行事件函数.
 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 文档,随便输入一些内容便可.测试用.
 示例 fla 文档:打开 CFEventExample.fla 文档.在 document class 处输入 CFEventExample .
 如图:
 
  保存文档. 测试 Flash 文档.在 XML 文档成功加载后会在输出面板中显示以下内容: [Event type="complete" bubbles=false cancelable=false eventPhase=2]示例2.使用 EventDispatcher 类:
 此示例文档详细:
 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 文档,随便输入一些内容便可.测试用.
 示例 fla 文档:打开 EventExample.fla 文档.在 document class 处输入 EventExample .保存文档.
 测试 Flash 文档.在 XML 文档成功加载后会在输出面板中显示以下内容: [HTTPStatusEvent type="httpStatus" bubbles=false cancelable=false eventPhase=2 status=200][Event type="complete" bubbles=false cancelable=false eventPhase=2]
 将 XML 文档地址改成错误的.然后再测试 Flash 文档.会输出下面的内容:
 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: 流错误。 URL: http://localhost/Example/AS3/events/Event/EventExamples.xml"]
 前面所讲的 EventDispatcher, Event, IOErrorEvent, HTTPStatusEvent, Timer 类,在 AS3 中成了内置类,
 EventDispatcher 没有 initialize 方法.但增加了一些其它的方法和属性.Event 等类也增加了一些方法和属性.
 大家也可以看看. 这里就不讲什么了.只是语法稍微不同.但基本跟前面讲的差不多.
 经典论坛讨论:本文链接:http://www.blueidea.com/tech/multimedia/2007/4832.asphttp://bbs.blueidea.com/thread-2768209-1-1.html
   出处:蓝色理想
责任编辑:蓝色月光
 上一页 [AS2]事件处理机制 -- 建立强大的事件处理机制 下一页 ◎进入论坛Flash专栏版块参加讨论
	      |