守望者--AIR技术交流

标题: 组件看不到?请使用本函数检测 [打印本页]

作者: 破晓    时间: 2015-1-8 09:59
标题: 组件看不到?请使用本函数检测
想看看大家,还有遇到其他情况没有,或其他意见,有的请回帖


  1. /**
  2. * 组件看不到?请使用本函数检测
  3. * @param target
  4. *
  5. */               
  6. public static function componentsNotShow(target:DisplayObject):void
  7. {
  8.         // 检查基本属性
  9.         if(!target.stage) {trace("没添加到舞台"); return;}
  10.         if(!target.visible) {trace("visible 是 false"); return;}
  11.         if(target.scaleX == 0) {trace("scaleX 为 0"); return;}
  12.         if(target.scaleY == 0) {trace("scaleY 为 0"); return;}
  13.         if(target.alpha == 0) {trace("目标是透明的(alpha 为 0)"); return;}
  14.         if(target.width == 0) {trace("宽度 为 0"); return;}
  15.         if(target.height == 0) {trace("高度 为 0"); return;}
  16.         
  17.         if(target.alpha < .3) trace("alpha 小于0.3");
  18.         if(target.width < 2) trace("宽度小于2");
  19.         if(target.height < 2) trace("高度小于2");
  20.         if(target.scrollRect) trace("设置了 scrollRect, 检查是否是该属性造成的");
  21.         
  22.         // 检查是否为特殊组件
  23.         var str:String;
  24.         if("text" in target)
  25.         {
  26.                 str = target["text"];
  27.                 if(str == null || str == "" || str.replace(/[                  ]+/, "") == "")
  28.                         trace("文本为空:", "text属性未赋值,或值为空格");
  29.         }
  30.         if("label" in target)
  31.         {
  32.                 str = target["label"];
  33.                 if(str == null || str == "" || str.replace(/[                  ]+/, "") == "")
  34.                         trace("label属性未赋值,或值为空格", "如果与label无关可以忽略该信息");
  35.         }
  36.         
  37.         // 检查位置
  38.         var p:Point = target.localToGlobal(new Point(0, 0));
  39.         if((p.x + target.width) < 0 || (p.y + target.height) < 0 || p.x > target.stage.stageWidth || p.y > target.stage.stageHeight)
  40.         {
  41.                 trace("目标位置在舞台之外");
  42.                 return;
  43.         }
  44.         var bound:Rectangle = target.transform.pixelBounds;
  45.         if(    bound.width < 2
  46.                 || bound.height < 2
  47.                 || bound.right < 0
  48.                 || bound.bottom < 0
  49.                 || bound.left > target.stage.stageWidth
  50.                 || bound.top > target.stage.stageHeight)
  51.         {
  52.                 trace("目标尺寸或位置有问题");
  53.         }
  54.         
  55.         // 截图检查透明区域
  56.         var bmp:BitmapData = new BitmapData(target.width, target.height, true, 0);
  57.         bmp.draw(target);
  58.         var ta:Number;
  59.         var alphaCount:int;
  60.         var alphaStatistics:Number = 0;
  61.         for(var w:int=0; w<bmp.width; w++)
  62.         {
  63.                 for(var h:int=0; h<bmp.height; h++)
  64.                 {
  65.                         ta = bmp.getPixel32(w, h) >>> 24;
  66.                         if(ta == 0) alphaCount++;
  67.                         
  68.                         alphaStatistics += ta/0xff;
  69.                 }
  70.         }
  71.         trace("目标平均透明度(alpha):", alphaStatistics/(w * h));
  72.         trace("目标透明部分占整体比例:", alphaCount / (w * h) * 100, "%", (alphaCount == (w * h))?"目标是透明的":"");
  73.         
  74.         // 把组件放到最上层
  75.         try
  76.         {
  77.                 var tempParent:DisplayObject = target;
  78.                 while(tempParent.parent)
  79.                 {
  80.                         tempParent.parent.setChildIndex(tempParent, tempParent.parent.numChildren - 1);
  81.                         tempParent = tempParent.parent;
  82.                 }
  83.                 trace("已把目标放在最上层");
  84.         }
  85.         catch(e:Error)
  86.         {
  87.                 trace("无法把目标放在最上层");
  88.         }
  89.         
  90.         trace("如果上述信息无法给予帮助,请做如下检查:");
  91.         trace("  · ", "查看目标位置是否为预定位置");
  92.         trace("  · ", "查看目标背景是否与大背景一致");
  93.         trace("  · ", "如果是文本组件,请检查text属性是否有值,color属性是否和背景一致");
  94.         trace("  · ", "如果你遇到的情况不是上述情况,请告知,以便更新此函数");
  95. }
复制代码







欢迎光临 守望者--AIR技术交流 (http://www.airmyth.com/)