當數字發生改變時,數字變大則呈現綠色的閃爍,變小則呈現紅色閃爍。
只是把以前JavaScript對DOM的操作,改用as3寫了一個共用類,不限於更改文字顏色。
package com.tool { import flash.utils.clearTimeout; import flash.utils.setTimeout; public class NotifyManager { public function NotifyManager() { } public var onUpdateFn:Function; public var onUpdateFnArgs:Array; public var onCompleteFn:Function; public var onCompleteFnArgs:Array; private var arr:Array; private var delayTimer:uint; /** * 銷燬方法 */ public function dispose():void { clearDelayTimer(); } /** * 播放 */ public function play():void { arr = timeArr.concat(); onUpdate(); } public function clear():void { dispose(); arr = null; onUpdateFn = null; onUpdateFnArgs = null; onCompleteFn = null; onCompleteFnArgs = null; } private function onUpdate():void { clearDelayTimer(); var t:Object = arr.shift(); if (t && t.t) { var args:Array; if (t.v) { args = onUpdateFnArgs.concat(true); } else { args = onUpdateFnArgs.concat(false); } onUpdateFn && onUpdateFn.apply(null, args); delayTimer = setTimeout(onUpdate, t.t * 1000); } else { onComplete(); } } private function onComplete():void { onCompleteFn && onCompleteFn.apply(null, onCompleteFnArgs); dispose(); } private function clearDelayTimer():void { if (delayTimer) { clearTimeout(delayTimer); delayTimer = 0; } } private static var timeArr:Array = [ {t: 0.07, v: 1}, {t: 0.07, v: 0}, {t: 0.07, v: 1}, {t: 0.07, v: 0}, {t: 0.07, v: 1}, {t: 0.07, v: 0}, {t: 0.07, v: 1}, {t: 0.07, v: 0} ]; private static var _notifyArr:Array = []; /** * 播放通知動畫 */ public static function notify(updateFn:Function=null, updateFnArgs:Array=null, completeFn:Function=null, completeFnArgs:Array=null):NotifyManager { var notify:NotifyManager = new NotifyManager(); notify.onUpdateFn = updateFn; notify.onUpdateFnArgs = updateFnArgs || []; notify.onCompleteFn = completeFn; notify.onCompleteFnArgs = completeFnArgs || []; _notifyArr.push(notify); notify.play(); return notify; } /** * 清除通知 */ public static function clear(notify:NotifyManager):void { if (notify) { for (var i:int = 0, len:int = _notifyArr.length; i < len; i++) { var _notify:NotifyManager = _notifyArr[i] as NotifyManager; if (_notify == notify) { _notifyArr.splice(i, 1); break ; } } notify.clear(); } } } }
使用方法也很簡單,只需要調用NotifyManager類的notify方法,傳入需要回調的函數(每次調用時的回調、完成時的回調,支持回傳參數)
以截圖中的demo爲例:
1: import com.tool.NotifyManager; 2: import flash.events.MouseEvent; 3: import flash.events.Event; 4: 5: btn1.buttonMode = true; 6: btn2.buttonMode = true; 7: 8: var txtNotify:NotifyManager; 9: 10: btn1.addEventListener(MouseEvent.CLICK, onClickHandler1); 11: btn2.addEventListener(MouseEvent.CLICK, onClickHandler2); 12: 13: function onClickHandler1(evt:MouseEvent):void 14: { 15: notifyHandler(0xFF0000); 16: } 17: 18: function onClickHandler2(evt:MouseEvent):void 19: { 20: notifyHandler(0x00FF00); 21: } 22: 23: function notifyHandler(color:uint):void 24: { 25: NotifyManager.clear(txtNotify); 26: 27: txtNotify = null; 28: txtNotify = NotifyManager.notify(changeTxtColorHandler, [color], null, null); 29: } 30: 31: function changeTxtColorHandler(color:uint, bool:Boolean):void 32: { 33: var tf:TextFormat = new TextFormat(); 34: tf.color = bool ? color : 0x000000; 35: 36: //txt.setStyle("textFormat", tf); 37: txt.setTextFormat(tf); 38: }
示例使用CS5.5編寫,需要使用Flash CS5.5打開。點此下載所有源碼>>