apache-flex - 在 Sprite 中获取应用于 Flash Sprite 的滤镜范围

标签 apache-flex flash actionscript sprite filter

我有一个 Flash 库,其中的 Sprite 符号由其他带有设计时应用滤镜的 Sprite 组成。我将这些符号嵌入到 Flex 应用程序中,如下所示:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:Script>
        <![CDATA[
            [Bindable]
            [Embed(source="Resources.swf", symbol="SquareContainer")]
            private var squareContainer_class:Class;

            private function log(msg:String):void {
                    output.text = output.text + "\n" + msg;
            }
        ]]>
    </mx:Script>

    <mx:VBox horizontalAlign="center" width="100%" height="100%" >
        <mx:Image id="squareContainer" source="{squareContainer_class}"/>
        <mx:Button click="log(squareContainer.width + ', ' + squareContainer.height);"/>
        <mx:TextArea id="output" width="100%" height="100%" />
    </mx:VBox>

</mx:Application>

在此示例中,SquareContainer 符号的宽度为 100 像素,高度为 100 像素;但是它包含一个带有发光和模糊滤镜的子 Sprite ,这导致 Sprite 看起来明显大于 100x100。由于我无法确定容器的组成,因此无法使用 BitmapData.generateFilterRect() 获取应用于嵌套 Sprite 的过滤器。

如何获取 Sprite 及其滤镜的大小?

最佳答案

哦,甜蜜的成功! (并感谢您的提示)一位 friend 用一个很好的递归函数帮助解决了这个问题,以处理嵌套 Sprite 上可能存在的过滤器:

private function getDisplayObjectRectangle(container:DisplayObjectContainer, processFilters:Boolean):Rectangle {
    var final_rectangle:Rectangle = processDisplayObjectContainer(container, processFilters);

    // translate to local
    var local_point:Point = container.globalToLocal(new Point(final_rectangle.x, final_rectangle.y));
    final_rectangle = new Rectangle(local_point.x, local_point.y, final_rectangle.width, final_rectangle.height);       

    return final_rectangle;
}

private function processDisplayObjectContainer(container:DisplayObjectContainer, processFilters:Boolean):Rectangle {
    var result_rectangle:Rectangle = null;

    // Process if container exists
    if (container != null) {
        var index:int = 0;
        var displayObject:DisplayObject;

        // Process each child DisplayObject
        for(var childIndex:int = 0; childIndex < container.numChildren; childIndex++){
            displayObject = container.getChildAt(childIndex);

            //If we are recursing all children, we also get the rectangle of children within these children.
            if (displayObject is DisplayObjectContainer) {

                // Let's drill into the structure till we find the deepest DisplayObject
                var displayObject_rectangle:Rectangle = processDisplayObjectContainer(displayObject as DisplayObjectContainer, processFilters);

                // Now, stepping out, uniting the result creates a rectangle that surrounds siblings
                if (result_rectangle == null) { 
                    result_rectangle = displayObject_rectangle.clone(); 
                } else {
                    result_rectangle = result_rectangle.union(displayObject_rectangle);
                }                       
            }                       
        }

        // Get bounds of current container, at this point we're stepping out of the nested DisplayObjects
        var container_rectangle:Rectangle = container.getBounds(container.stage);

        if (result_rectangle == null) { 
            result_rectangle = container_rectangle.clone(); 
        } else {
            result_rectangle = result_rectangle.union(container_rectangle);
        }


        // Include all filters if requested and they exist
        if ((processFilters == true) && (container.filters.length > 0)) {
            var filterGenerater_rectangle:Rectangle = new Rectangle(0,0,result_rectangle.width, result_rectangle.height);
            var bmd:BitmapData = new BitmapData(result_rectangle.width, result_rectangle.height, true, 0x00000000);

            var filter_minimumX:Number = 0;
            var filter_minimumY:Number = 0;

            var filtersLength:int = container.filters.length;
            for (var filtersIndex:int = 0; filtersIndex < filtersLength; filtersIndex++) {                      
                var filter:BitmapFilter = container.filters[filtersIndex];

                var filter_rectangle:Rectangle = bmd.generateFilterRect(filterGenerater_rectangle, filter);

                filter_minimumX = filter_minimumX + filter_rectangle.x;
                filter_minimumY = filter_minimumY + filter_rectangle.y;

                filterGenerater_rectangle = filter_rectangle.clone();
                filterGenerater_rectangle.x = 0;
                filterGenerater_rectangle.y = 0;

                bmd = new BitmapData(filterGenerater_rectangle.width, filterGenerater_rectangle.height, true, 0x00000000);                      
            }

            // Reposition filter_rectangle back to global coordinates
            filter_rectangle.x = result_rectangle.x + filter_minimumX;
            filter_rectangle.y = result_rectangle.y + filter_minimumY;

            result_rectangle = filter_rectangle.clone();
        }               
    } else {
        throw new Error("No displayobject was passed as an argument");
    }

    return result_rectangle;
}

关于apache-flex - 在 Sprite 中获取应用于 Flash Sprite 的滤镜范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/466280/

相关文章:

apache-flex - 从 ActionScript 影片剪辑生成 flv、mpg 或其他一些影片格式

java - 如何将 Java 方法调用到 Adob​​e Flex 移动应用程序中

apache-flex - 将 RGB 转换为十六进制

flash - Youtube 视频嵌入不工作 : "Movie Not Loaded..."?

android - 如何减少包含 1,000 个资源的 AIR Flash 应用程序中的内存 (RAM) 使用量?

actionscript - 这个 Javascript 代码 ":"上的 "var switchToTarget : Transform;"(冒号符号)是什么意思?

java - Flex Spring 集成

javascript - 有没有办法用 javascript 提取 flash 对象的状态?

flash - 在 Flash 中使用 ExternalInterface

flash - 每个结束时按顺序播放声音