梦星痕

--记录生活与工作中的点点滴滴
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

【备份】一些平时在用的类,有些是收集,有些原创

Posted on 2013-02-22 15:53  梦星痕  阅读(80)  评论(0)    收藏  举报

一、对象亮度及对比度调节。

 ActionScript 代码
 1 package 
 2 {
 3     /**
 4      * 一些辅助功能扩展
 5      */
 6     public class EUtils 
 7     {        
 8         /** 取得传入的字串的长度(按英文占1个字符,中文占2个字符来统计)**/
 9         public static function getStringLength(str:String):Number
10         {
11             var maxLength:Number = 0;
12             for(var i:int = 0; i < str.length; i++)
13             {
14                 if(str.charCodeAt(i) < 127)
15                 {
16                     maxLength ++;
17                 }
18                 else
19                 {
20                     maxLength += 2;
21                 }
22             }
23             return maxLength;
24         }
25         
26         /**
27          * 将字串剪切成指定的长度(传入值以字节为单位,汉字占双字节)
28          * 
29          * @param    str        原始字串
30          * @param    len        要截取的长度
31          * @return    截取后的字串
32          */
33         public static function getStringByLength(str:String, len:uint):String
34         {
35             if (getStringLength(str) <= len)
36             {
37                 return str;
38             }
39             var newStr:String = "";
40             for (var i:int = 0; i < str.length; i++)
41             {
42                 if (getStringLength(str.substring(0, i + 1)) > len)
43                 {
44                     newStr = str.substring(0, i);
45                     break;
46                 }
47             }
48             return newStr;
49         }
50 
51         /**
52          * 将一个颜色值转换成一个 ColorTransform 对象
53          * 使用方法: spr.transform.colorTransform = ETools.ColorToColorTransform(col);
54          * @param    col
55          * @return
56          */
57         public static function ColorToColorTransform(col:uint):ColorTransform 
58         {
59             var cStr:String = ("000000" + col.toString(16)).substr(-6);
60             var r:int = int("0x" + cStr.substr(0,2));
61             var g:int = int("0x" + cStr.substr(2,2));
62             var b:int = int("0x" + cStr.substr(4,2));
63             
64             return new ColorTransform(1, 1, 1, 1, r, g, b);
65         }
66         
67         /**
68          * 将一个 ColorTransform 对象转换成一个 uint 型的颜色值(忽略透明度)
69          * 使用方法: var col:uint = ETools.ColorTransformToColor(spr.transform.colorTransform);
70          * @param    c
71          * @return
72          */
73         public static function ColorTransformToColor(c:ColorTransform):uint
74         {            
75             var r:String = ("00" + c.redOffset.toString(16)).substr( -2);
76             var g:String = ("00" + c.greenOffset.toString(16)).substr( -2);
77             var b:String = ("00" + c.blueOffset.toString(16)).substr( -2);
78             
79             return int("0x" + r + g + b);
80         }
81     }
82 }

 

二、简单的独立Tips对象。

ActionScript 代码
  1 package org.dstart.utils 
  2 {
  3     import com.adobe.utils.ArrayUtil;
  4     import flash.display.DisplayObject;
  5     import flash.display.InteractiveObject;
  6     import flash.display.Sprite;
  7     import flash.events.EventDispatcher;
  8     import flash.events.MouseEvent;
  9     
 10     /**
 11      * 按钮或相应对象的提示 Tips(暂时只支持 弹出方式为右下角滑动出现)
 12      */
 13     public class ETips extends EventDispatcher 
 14     {
 15         private static var tipsArray:Array = [];        /** Tips 对象类 **/
 16         private static var tipsBG:Sprite = null;        /** Tips 对象的默认背景对象 **/
 17         
 18         /**
 19          * 设置 Tips 对象的全局背景面板(针对特定对象)
 20          * 该方法目前暂时只支持初始时调用
 21          * @param    spr
 22          */
 23         public static function SetTipsBackground(spr:Sprite):void
 24         {
 25             tipsBG = spr;
 26         }
 27         
 28         /**
 29          *为一个指定的 InteractiveObject设置  Tips 显示
 30          *         
 31          * @param obj
 32          * @param tipsInfo
 33          * @param iconPath
 34          * 
 35         */
 36         public static function setTips(obj:InteractiveObject, tipsInfo:String, iconPath:String = ""):void
 37         {            
 38             var tData:TipsData = getTipsData(obj);
 39             if(tData)
 40             {
 41                 tData.tipDlg.setData(tipsInfo);
 42             }
 43             else
 44             {
 45                 var subTip:IconTipsDlg = new IconTipsDlg();
 46                 subTip.setBackground(tipsBG);
 47                 subTip.setData(tipsInfo);                
 48                 
 49                 tData = new TipsData();
 50                 tData.tipTarget = obj;
 51                 tData.tipDlg = subTip;
 52                 tipsArray.push(tData);
 53                 
 54                 SetTipsListener(obj);                
 55             }
 56         }
 57         
 58         /** 删除指定对象的Tips对象 **/
 59         public static function removeTips(obj:InteractiveObject):void
 60         {
 61             for(var i:int = 0; i < tipsArray.length; i++)
 62             {
 63                 var sub:TipsData = tipsArray[i];
 64                 if(sub)
 65                 {
 66                     if(sub.tipTarget == obj)
 67                     {
 68                         DelTipsListener(sub.tipTarget);
 69                         sub.tipDlg.deleteTips();
 70                         tipsArray[i] = null;
 71                     }
 72                 }
 73             }
 74 
 75             var len:uint = tipsArray.length;
 76             
 77             for(var ii:int = len; i > -1; i--)
 78             {
 79                 if(tipsArray[i] === null)
 80                 {
 81                     tipsArray.splice(i, 1);
 82                 }
 83             }        
 84         }
 85         
 86         /** 根据传入的数据,取到一个 TipsData 类对象 **/
 87         private static function getTipsData(obj:InteractiveObject):TipsData
 88         {
 89             if(obj)
 90             {
 91                 for(var i:int = 0; i < tipsArray.length; i++)
 92                 {
 93                     var sub:TipsData = tipsArray[i];
 94                     if(sub)
 95                     {
 96                         if(sub.tipTarget == obj)
 97                         {
 98                             return sub;
 99                         }
100                     }
101                 }
102             }
103             return null;
104         }
105         
106         private static function SetTipsListener(obj:InteractiveObject):void
107         {
108             obj.addEventListener(MouseEvent.ROLL_OVER, onShowTipsEvent);
109             obj.addEventListener(MouseEvent.ROLL_OUT, onHiddenTipsEvent);
110             obj.addEventListener(MouseEvent.MOUSE_DOWN, onHiddenTipsEvent);
111         }
112         
113         private static function DelTipsListener(obj:InteractiveObject):void
114         {
115             obj.removeEventListener(MouseEvent.ROLL_OVER, onShowTipsEvent);
116             obj.removeEventListener(MouseEvent.ROLL_OUT, onHiddenTipsEvent);
117             obj.removeEventListener(MouseEvent.MOUSE_DOWN, onHiddenTipsEvent);
118         }
119         
120         private static function onShowTipsEvent(e:MouseEvent):void
121         {            
122             var tData:TipsData = getTipsData(e.currentTarget as InteractiveObject);
123             if(tData)
124             {
125                 if(tData.tipTarget)
126                 {
127                     if(tData.tipTarget.stage)
128                     {                         
129                         tData.tipTarget.stage.addChild(tData.tipDlg as DisplayObject);
130                         tData.tipDlg.showTips();
131                     }
132                 }                
133             }
134         }
135         
136         private static function onHiddenTipsEvent(e:MouseEvent):void
137         {
138             var tData:TipsData = getTipsData(e.currentTarget as InteractiveObject);
139             if(tData)
140             {
141                 if (e.type == MouseEvent.MOUSE_DOWN)
142                 {
143                     tData.tipDlg.visible = false;
144                 }
145                 tData.tipDlg.hiddenTips();
146             }
147         }
148     }
149 }
150 
151 import flash.display.CapsStyle;
152 import flash.display.InteractiveObject;
153 import flash.display.JointStyle;
154 import flash.display.LineScaleMode;
155 import flash.display.MovieClip;
156 import flash.display.Sprite;
157 import flash.events.Event;
158 import flash.events.TimerEvent;
159 import flash.filters.DropShadowFilter;
160 import flash.text.TextField;
161 import flash.utils.Timer;
162 
163 class TipsData
164 {
165     public var tipTarget:InteractiveObject;        /** Tips 对象的发起类 **/
166     public var tipDlg:MovieClip;                    /** Tips 影片对象类 **/
167 }
168 
169 
170 class IconTipsDlg extends MovieClip
171 {
172     private var _txtInfo:TextField;        /** 信息显示文字框 **/
173     private var _sprBG:Sprite;            /** 背景图片板 **/
174     
175     private var _isCreateBG:Boolean = true;    /** 是否要新建一个背景面板 **/
176 
177     private var sprObject:Sprite;        /** 用于摆放所有的对象 **/
178     private var baseWidth:int;            /** 默认的背景宽度 **/
179     private var baseHeight:int;            /** 默认的背景高度 **/
180 
181     public function IconTipsDlg():void
182     {
183         sprObject = new Sprite();
184         addChild(sprObject);        
185         
186         _txtInfo = new TextField();
187         _txtInfo.height = 20;
188         _txtInfo.width = 20;
189         _txtInfo.textColor = 0x000000;// FFFFFF;        
190         sprObject.addChild(_txtInfo);
191         
192         sprObject.filters = [ new DropShadowFilter(3, 45, 0, 0.35, 3, 3, 2) ];
193         
194         setBackground(null);
195     }
196     
197     public function setBackground(spr:Sprite):void
198     {
199         if (spr)
200         {
201             if (_sprBG)
202             {
203                 _sprBG.graphics.clear();
204             }
205             _isCreateBG = false;
206             
207             _sprBG = spr;
208             sprObject.addChild(_sprBG);
209             sprObject.addChild(_txtInfo);
210         }
211         else
212         {
213             _isCreateBG = true;
214             
215             _sprBG = new Sprite();
216             sprObject.addChild(_sprBG);
217             sprObject.addChild(_txtInfo);
218         }
219     }
220     
221     
222     /** 设置数据值(先清空之前显示的对象) **/
223     public function setData(str:String):void
224     {        
225         /** 清空之前的对象 **/
226         _txtInfo.text = "";    
227         
228         _txtInfo.x = 5;
229         _txtInfo.y = 2;
230         _txtInfo.width = 1000;
231         _txtInfo.htmlText = str;// "<font face='tahoma'>" + str + "</font>";
232         _txtInfo.width = _txtInfo.textWidth + 6;
233         
234         if (_isCreateBG)
235         {
236             _sprBG.graphics.clear();    
237             _sprBG.graphics.lineStyle(1, 0, 1, true, LineScaleMode.NORMAL, CapsStyle.ROUND, JointStyle.ROUND, 5);
238             _sprBG.graphics.beginFill(0xF7EFDE, 1);
239             _sprBG.graphics.drawRoundRectComplex(0, 0, _txtInfo.width + 8, 21, 5, 5, 5, 5);
240             _sprBG.graphics.endFill();
241         }
242         else
243         {
244             _sprBG.width = _txtInfo.width + 8;            
245         }
246         
247         baseWidth = _sprBG.width;
248         baseHeight = _sprBG.height;        
249     }
250     
251     /** 显示面板 **/
252     private var waitFrame:int = 0; 
253     public function showTips(waitTime:Number = 0.5):void
254     {
255         if(this.hasEventListener(Event.ENTER_FRAME))
256         {
257             this.removeEventListener(Event.ENTER_FRAME, onCheckShowEvent);
258             this.removeEventListener(Event.ENTER_FRAME, onCheckHiddenEvent);
259         }
260         
261         if(stage)
262         {
263             waitFrame = waitTime * stage.frameRate;
264             
265             sprObject.visible = false;
266             
267             this.addEventListener(Event.ENTER_FRAME, onCheckShowEvent);                
268         }
269     }
270     
271     private function onCheckShowEvent(e:Event):void
272     {
273         if(_txtInfo.text.replace(/ /g, "") == "")
274         {            
275             return;  //没有任何文字信息的话,就不显示
276         }
277         if(--waitFrame <= 0)
278         {
279             this.removeEventListener(Event.ENTER_FRAME, onCheckShowEvent);                
280             this.addEventListener(Event.ENTER_FRAME, onCheckHiddenEvent);
281             
282             var px:int = this.stage.mouseX + 12;
283             var py:int = this.stage.mouseY + 22;              
284             
285             if(px + baseWidth > stage.stageWidth)
286             {
287                 sprObject.x = -baseWidth - 24;
288             }
289             if(py + baseHeight > stage.stageHeight)
290             {
291                 sprObject.y = -baseHeight - 24;
292             }                
293             
294             this.x = px;
295             this.y = py;
296             
297             sprObject.visible = true;
298             
299             Show();
300         }            
301     }
302     
303     private function onCheckHiddenEvent(e:Event):void
304     {
305         if (++waitFrame > 3 * stage.frameRate)
306         {
307             hiddenTips();
308         }            
309     }
310     
311     /** 隐藏面板 **/
312     public function hiddenTips():void
313     {
314         this.removeEventListener(Event.ENTER_FRAME, onCheckShowEvent);
315         this.removeEventListener(Event.ENTER_FRAME, onCheckHiddenEvent);
316         
317         Hidden();
318     }
319     
320     /** 删除 Tips 对象 **/
321     public function deleteTips():void
322     {            
323         destroy();
324     }
325     
326     /** 重写删除方法 **/
327     public function destroy():void
328     {
329         hiddenTips();            
330         RemoveSelf();
331     }
332     
333     //////////////////////////////////////////////////////////////////////////////////////////////
334     
335     protected var m_isPlayMovie:Boolean = true;
336     
337     /** 显示 **/        
338     public function Show():void
339     {
340         visible = true;
341         parent.addChild(this);        
342         
343         if (m_isPlayMovie)
344         {
345             scaleX = scaleY = 0.05;
346             
347             var movTime:Timer = new Timer(1, 5);
348             movTime.addEventListener(TimerEvent.TIMER, onShowEvent);
349             movTime.addEventListener(TimerEvent.TIMER_COMPLETE, onDoneEvent);
350             movTime.start();
351         }
352         else
353         {
354             scaleX = scaleY = 1;            
355         }
356     }
357     
358     private function onShowEvent(e:TimerEvent):void 
359     {
360         scaleX = (scaleX < 1) ? scaleX + 0.2 : 1;
361         scaleY = (scaleY < 1) ? scaleY + 0.2 : 1;
362                     
363         e.updateAfterEvent();
364     }
365     
366     private function onDoneEvent(e:TimerEvent):void 
367     {
368         var movTime:Timer = e.currentTarget as Timer;
369         if (movTime)
370         {                
371             movTime.stop();
372             movTime.removeEventListener(TimerEvent.TIMER, onShowEvent);
373             movTime.removeEventListener(TimerEvent.TIMER_COMPLETE, onDoneEvent);
374             movTime = null;
375         }        
376     }
377     
378     
379     /** 隐藏 **/
380     public function Hidden(isDelete:Boolean = false):void
381     {    
382         if (m_isPlayMovie)
383         {
384             var movTime:Timer = new Timer(1, 5);
385             movTime.addEventListener(TimerEvent.TIMER, onHiddenEvent);
386             
387             if(isDelete)
388             {                
389                 movTime.addEventListener(TimerEvent.TIMER_COMPLETE, onDestroyEvent);                
390             }
391             else
392             {                
393                 movTime.addEventListener(TimerEvent.TIMER_COMPLETE, onHiddenDoneEvent);                                
394             }
395             
396             movTime.start();
397         }
398         else
399         {
400             visible = false;
401             scaleX = scaleY = 1;
402             if (isDelete)
403             {
404                 destroy();
405             }
406         }
407     }
408     
409     private function onHiddenEvent(e:TimerEvent):void 
410     {            
411         scaleX = (scaleX > 0.05) ? scaleX - 0.2 : 0.05;
412         scaleY = (scaleY > 0.05) ? scaleY - 0.2 : 0.05;
413         
414         e.updateAfterEvent();
415     }
416     
417     private function onDestroyEvent(e:TimerEvent):void 
418     {
419         var movTime:Timer = e.currentTarget as Timer;
420         if (movTime)
421         {                
422             movTime.stop();
423             movTime.removeEventListener(TimerEvent.TIMER, onHiddenEvent);
424             movTime.removeEventListener(TimerEvent.TIMER_COMPLETE, onDestroyEvent);
425             movTime = null;
426         }
427         
428         destroy();
429     }
430     
431     private function onHiddenDoneEvent(e:TimerEvent):void 
432     {
433         visible = false;
434         
435         var movTime:Timer = e.currentTarget as Timer;
436         if (movTime)
437         {                
438             movTime.stop();
439             movTime.removeEventListener(TimerEvent.TIMER, onHiddenEvent);
440             movTime.removeEventListener(TimerEvent.TIMER_COMPLETE, onHiddenDoneEvent);
441             movTime = null;
442         }
443     }
444     
445     /** 将当前对象移动到屏幕正中间  **/
446     public function moveToCenter():void
447     {
448         if(stage)
449         {                
450             this.x = int((stage.stageWidth - width)/2 + width/2);
451             this.y = int((stage.stageHeight - height)/2 + height/2);            
452         }
453     }
454     
455     /** 删除自己 **/
456     private final function RemoveSelf():void
457     {
458         if(parent)
459         {
460             parent.removeChild(this);
461         }
462     }
463 }

 

三、简单的独立Loading条。

View Code
 1 package 
 2 {
 3     import flash.display.Sprite;
 4     import flash.text.TextField;
 5     import flash.text.TextFormat;
 6     import org.dstart.air.csuper.CSprite;
 7     
 8     /**
 9      * 下载进度条类
10      *
11      **/     
12     public class LoadingObject extends CSprite
13     {
14         private var spr:Sprite;
15         private var txt:TextField;
16         
17         private var fillColor:uint = 0;
18         
19         public function LoadingObject(fColor:uint = 0x000000)
20         {
21             fillColor = fColor;
22             
23             spr = new Sprite();
24             txt = new TextField();
25             
26             txt.x = -53;
27             txt.y = 7;
28             txt.width = 106;
29             txt.height = 20;
30             txt.selectable = false;
31 
32             addChild(spr);
33             addChild(txt);
34         }    
35          
36         /** 设置下载百分比 **/
37         public function set percent(num:Number):void
38         {
39             spr.graphics.clear();
40             spr.graphics.lineStyle(1, fillColor);
41             spr.graphics.drawRect(-53, -6, 106, 12);
42             spr.graphics.endFill();
43             
44             spr.graphics.beginFill(fillColor);
45             spr.graphics.drawRect(-50, -3, num, 6);
46             spr.graphics.endFill();
47     
48             txt.textColor = fillColor;
49             txt.htmlText = "<p align='center'>" + String("  " + num).substr(-3) + "%</p>";
50         }
51         
52         /** 删除自己 **/
53         override public function destroy():void
54         {
55             super.destroy();
56             RemoveSelf();
57         }
58     }
59 }