前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >文字闪烁提示

文字闪烁提示

作者头像
meteoric
发布2018-11-16 17:41:44
发布2018-11-16 17:41:44
1.8K00
代码可运行
举报
文章被收录于专栏:游戏杂谈游戏杂谈
运行总次数:0
代码可运行

当数字发生改变时,数字变大则呈现绿色的闪烁,变小则呈现红色闪烁。

只是把以前JavaScript对DOM的操作,改用as3写了一个共用类,不限于更改文字颜色。

代码语言:javascript
代码运行次数:0
复制
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为例:

代码语言:javascript
代码运行次数:0
复制
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打开。点此下载所有源码>>

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2012-11-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档