当数字发生改变时,数字变大则呈现绿色的闪烁,变小则呈现红色闪烁。
只是把以前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();
}
}
}
}
以截图中的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: }