生成和處理事件機(jī)制
在以往的as2當(dāng)中,,基本上只有組件能夠發(fā)送事件,平時的程序中基本上都是這種類似01.xxx.onLoad=function(){} 的回調(diào)函數(shù)形式,,甚至利用onEnterFrame來模擬不斷偵聽的效果,,而as3中提供的解決方案是DOM3事件處理機(jī)制,它包括注冊偵聽器,、發(fā)送事件,、偵聽事件、移除偵聽器4個步驟,。 這是一個小例子:
下載: Teacher.as 01.//事件發(fā)送者類
02.package {
03.import flash.events.EventDispatcher;
04.public class Teacher extends EventDispatcher
{ 05. 06.public var name:String; 07. 08.public function Teacher(nm:String) { 09.name = nm; 10.} 11. 12.public function teach(txt:Object,value:String):void{ 13.var teachevent:TeachEvent = new TeachEvent(); 14.teachevent.lesson = value; 15.txt.text = teachevent; 16.dispatchEvent(teachevent); 17.} 18.} 19.} 那么這個TeachEvent到底是什么呢,?其實就是我們自定義的一個事件類,首先這個類要繼承Event事件類,,Event的構(gòu)造函數(shù)是這樣的:Event(type:String, bubbles:Boolean = false, cancelable:Boolean = false) ;再利用public static const建立一個事件類型,,因此我們在TeachEvent的構(gòu)造函數(shù)中需要加上這句:super(TEACH_LESSON);他就意味著將事件類型TEACH_LESSON傳遞給父類Event的構(gòu)造函數(shù)。
然后需要重寫toString和clone這兩個方法,,在重寫toString方法的時候需要用到formatToString,,對于formatToString官方的解釋是:在自定義 Event 類中實現(xiàn) toString() 方法的實用程序函數(shù)。其實說白了將toString的返回結(jié)果按自定義的格式覆蓋了一遍,。之后在重寫clone方法時需要注意的一點是為了和實例狀態(tài)保持一致,,不要忘了將自定義的方法寫進(jìn)去。 下載: TeachEvent.as 01.package { 02.import flash.events.Event; 03.public class TeachEvent extends Event { 04. 05.public static const TEACH_LESSON:String = "講課"; 06.private var _lesson:String; 07. 08.public function TeachEvent() { 09.super(TEACH_LESSON); 10.} 11. 12.public function set lesson(ls:String):void{ 13._lesson = ls; 14.} 15. 16.public function get lesson():String{ 17.return _lesson; 18.} 19. 20.override public function toString():String{ 21.return formatToString("TeachEvent","type","bubbles","cancelable","eventPhase","lesson"); 22.} 23. 24.override public function clone():Event{ 25.var teachEvent:TeachEvent = new TeachEvent(); 26.teachEvent._lesson = _lesson; 27.return teachEvent; 28.} 29.} 30.} 這里我建了一個Student類,,比較簡單,,在偵聽方法listenTeacher里面為什么接受的參數(shù)是(e:TeachEvent,…arg)后面會講到。 下載: Student.as 下載: EventExample.as
01.package { 02.import flash.text.TextField; 03.import flash.display.Sprite; 04.import flash.display.MovieClip; 05.import flash.events.MouseEvent; 06.import flash.events.Event; 07.public class EventExample extends Sprite { 08. 09.private var teacher:Teacher; 10.private var student:Student; 11.var Handler:Function; 12.public function EventExample() { 13.teacher = new Teacher("老師"); 14.student = new Student(); 15. 16.//學(xué)生在老師那里注冊了“講課”事件的偵聽,執(zhí)行了listenTeacher 17.Handler = JEventDelegate.create(student.listenTeacher,txt3,txt1.text); 18.teacher.addEventListener(TeachEvent.TEACH_LESSON,Handler); 19. 20.txt1.addEventListener(Event.CHANGE,changeHandler); 21.teachBtn.buttonMode = true; 22.teachBtn.addEventListener(MouseEvent.CLICK,click); 23.} 24.private function changeHandler(e:Event){ 25.Handler = JEventDelegate.create(student.listenTeacher,txt3,txt1.text); 26.} 27.private function click(e:MouseEvent):void{ 28.teacher.removeEventListener(TeachEvent.TEACH_LESSON,Handler); 29.teacher.addEventListener(TeachEvent.TEACH_LESSON,Handler); 30.teacher.teach(txt2,txt1.text); 31.} 32.} 33.} ok,,大致思路就是這樣,,這里我要捎帶的提一下JEventDelegate類,是一個網(wǎng)友寫的,,這里我借用一下,。它的主要作用就是為了解決給偵聽器傳參的問題,這就是為什么我前面的偵聽器函數(shù)要寫成Handler = JEventDelegate.create(student.listenTeacher,txt3,txt1.text);這種形式的原因,,student.listenTeacher是我們自定義的偵聽器函數(shù),,后面兩個是需要傳入的參數(shù)。因此在Student類的偵聽方法里面需要寫成listenTeacher(e:TeachEvent,…arg),?!璦rg是一個對象,把傳入的參數(shù)都按順序保存進(jìn)來了,,我們只要像訪問數(shù)組一樣訪問它就可以了,。 下載: JEventDelegate.as 01.package { 02.public class JEventDelegate { 03.public function JEventDelegate() { 04.} 05.public static function create(f:Function,...arg):Function { 06.var F:Boolean=false; 07.var _f:Function=function(e:*,..._arg){ 08. _arg=arg 09.if(!F){ 10.F=true 11._arg.unshift(e) 12.} 13.f.apply(null,_arg) 14. }; 15.return _f; 16.} 17.public static function toString():String { 18.return "Class JEventDelegate"; 19.} 20.} 21.} 以上就是我的個人心得,如果有理解不當(dāng)?shù)牡胤?,歡迎各位visiters批評指正,。 下載源文件:EventExample.rar
|
|
來自: 靈巖俠客 > 《flashas3.0》