11RIA 闪客社区 - 最赞 Animate Flash 论坛

搜索
查看: 1947|回复: 1
上一主题 下一主题

[代码优化 & 调试] AS3舞台对象,看不到?请使用本函数检测

[复制链接] TA的其它主题
发表于 2019-2-7 17:20:31 | 显示全部楼层 |阅读模式

【游客模式】——注册会员,加入11RIA 闪客社区吧!一起见证Flash的再次辉煌……

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
转载:破晓大神博客
网址:http://www.code2048.net/45.html


[Actionscript3] 纯文本查看 复制代码
/**
* 组件看不到?请使用本函数检测 
* @param target
* @author 破晓([url]www.code2048.net[/url])
* 
*/                
public static function componentsNotShow(target:DisplayObject):void
{
        // 检查基本属性
        if(!target.stage) {trace("没添加到舞台"); return;}
        if(!target.visible) {trace("visible 是 false"); return;}
        if(target.scaleX == 0) {trace("scaleX 为 0"); return;}
        if(target.scaleY == 0) {trace("scaleY 为 0"); return;}
        if(target.alpha == 0) {trace("目标是透明的(alpha 为 0)"); return;}
        if(target.width == 0) {trace("宽度 为 0"); return;}
        if(target.height == 0) {trace("高度 为 0"); return;}
        
        if(target.alpha < .3) trace("alpha 小于0.3");
        if(target.width < 2) trace("宽度小于2");
        if(target.height < 2) trace("高度小于2");
        if(target.scrollRect) trace("设置了 scrollRect, 检查是否是该属性造成的");
        
        // 检查是否为特殊组件
        var str:String;
        if("text" in target)
        {
                str = target["text"];
                if(str == null || str == "" || str.replace(/[                  ]+/, "") == "")
                        trace("文本为空:", "text属性未赋值,或值为空格");
        }
        if("label" in target)
        {
                str = target["label"];
                if(str == null || str == "" || str.replace(/[                  ]+/, "") == "")
                        trace("label属性未赋值,或值为空格", "如果与label无关可以忽略该信息");
        }
        
        // 检查位置
        var p:Point = target.localToGlobal(new Point(0, 0));
        if((p.x + target.width) < 0 || (p.y + target.height) < 0 || p.x > target.stage.stageWidth || p.y > target.stage.stageHeight) 
        {
                trace("目标位置在舞台之外"); 
                return;
        }
        var bound:Rectangle = target.transform.pixelBounds;
        if(    bound.width < 2 
                || bound.height < 2 
                || bound.right < 0 
                || bound.bottom < 0 
                || bound.left > target.stage.stageWidth 
                || bound.top > target.stage.stageHeight)
        {
                trace("目标尺寸或位置有问题"); 
        }
        
        // 截图检查透明区域
        var bmp:BitmapData = new BitmapData(target.width, target.height, true, 0);
        bmp.draw(target);
        var ta:Number;
        var alphaCount:int;
        var alphaStatistics:Number = 0;
        for(var w:int=0; w<bmp.width; w++)
        {
                for(var h:int=0; h<bmp.height; h++)
                {
                        ta = bmp.getPixel32(w, h) >>> 24;
                        if(ta == 0) alphaCount++;
                        
                        alphaStatistics += ta/0xff;
                }
        }
        trace("目标平均透明度(alpha):", alphaStatistics/(w * h));
        trace("目标透明部分占整体比例:", alphaCount / (w * h) * 100, "%", (alphaCount == (w * h))?"目标是透明的":"");
        
        // 把组件放到最上层
        try
        {
                var tempParent:DisplayObject = target;
                while(tempParent.parent)
                {
                        tempParent.parent.setChildIndex(tempParent, tempParent.parent.numChildren - 1);
                        tempParent = tempParent.parent;
                }
                trace("已把目标放在最上层");
        }
        catch(e:Error)
        {
                trace("无法把目标放在最上层");
        }
        
        trace("如果上述信息无法给予帮助,请做如下检查:");
        trace("  · ", "查看目标位置是否为预定位置");
        trace("  · ", "查看目标背景是否与大背景一致");
        trace("  · ", "如果是文本组件,请检查text属性是否有值,color属性是否和背景一致");
        trace("  · ", "如果你遇到的情况不是上述情况,请告知,以便更新此函数");
}
发表于 2019-2-8 07:48:57 | 显示全部楼层
收藏!!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐 上一条 /1 下一条

感谢所有支持论坛的朋友:下面展示最新的5位赞助和充值的朋友……更多赞助和充值朋友的信息,请查看:永远的感谢名单

SGlW(66139)、 anghuo(841)、 whdsyes(255)、 longxia(60904)、 囫囵吞澡(58054)

下面展示总排行榜的前3名(T1-T3)和今年排行榜的前3名的朋友(C1-C3)……更多信息,请查看:总排行榜今年排行榜

T1. fhqu1462(969)、 T2. lwlpluto(14232)、 T3. 1367926921(962)  |  C1. anghuo(147)、 C2. fdisker(27945)、 C3. 囫囵吞澡(58054)



快速回复 返回顶部 返回列表