增加反射物体的高度

时间:2014-03-20 10:50:06

标签: actionscript-3 flex flex4.5

想要反映图像,我的代码看起来像

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx"
                       height="500" width="500"
                       creationComplete="init(event)">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <fx:Script>
        <![CDATA[

            import flash.filters.BlurFilter;

            import mx.core.UIComponent;
            import mx.events.FlexEvent;

            private var loader:Loader = new Loader();
            private var picture:BitmapData;

            private var reflection:BitmapData;
            private var reflectionHolder:Bitmap;

            protected function init(event:FlexEvent):void
            {
                loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
                loader.load(new URLRequest("assets/rectangle.png"));
            }

            private function onComplete (event:Event):void
            {
                trace("loaded!");
                var loaderInfo:LoaderInfo = LoaderInfo(event.target);
                picture = new BitmapData(loaderInfo.width, loaderInfo.height, false, 0xFFFFFF);
                picture.draw(loaderInfo.loader);
                img.source=picture;
                trace(picture.height,picture.width);
            }


            private function createReflection():void 
            {
                reflection=new BitmapData(picture.width,picture.height,true,0x00ffffff);

                var flipMatrix:Matrix = new Matrix();
                flipMatrix.rotate(Math.PI);
                flipMatrix.scale( -1, 1 );
                flipMatrix.translate(0, picture.height);

                reflection.draw(picture, flipMatrix);

                for (var i:int = 0; i < picture.height; i++) 
                {
                    var rowFactor:Number = Math.max(0, 1 - (i / picture.height));
                    for (var j:int = 0; j < picture.width; j++) 
                    {
                        var pixelColor:uint = reflection.getPixel32(j,i);
                        var pixelAlpha:uint = pixelColor>>24&0xff;
                        var pixelRGB:uint = pixelColor&0xffffff;
                        var resultAlpha:uint = pixelAlpha*rowFactor;
                        reflection.setPixel32(j, i, resultAlpha << 24 | pixelRGB);
                    }
                }

                reflection.applyFilter(reflection, reflection.rect, new Point(0, 0), new BlurFilter(4, 4, 3));

                reflectionHolder=new Bitmap(reflection);
                reflectionHolder.y=img.y+img.height;
                reflectionHolder.x=img.x;
                reflectionHolder.width = img.width;
                reflectionHolder.height = img.height;

                var uIComponent:UIComponent = new UIComponent();
                uIComponent.addChild(reflectionHolder);
                this.addElement(uIComponent);
            }

            protected function btn_clickHandler(event:MouseEvent):void
            {
                createReflection();
            }
        ]]>
    </fx:Script>
        <s:Group height="100%" width="100%">
            <s:BorderContainer backgroundColor="0x0d0d0d" height="100%" width="100%"/>
            <s:Image id="img" height="100" width="200" />
            <s:Button id="btn" click="btn_clickHandler(event)" />
        </s:Group>
</s:WindowedApplication>

它显示了结果,但输出并不完美...

enter image description here

目标: - 反射的物体应与真实物体相同,并与真实物体有一定距离。

2 个答案:

答案 0 :(得分:1)

在一个答案中显示的代码非常多。实际上,我搜索了我的旧档案,然后我找到了我的旧课程进行反思(2009年)。它将为您创建反射,它支持动画对象,以及从对象到反射开始的距离。只有两行代码:

var test:Sprite = new Sprite();
test.graphics.beginFill(0x990099);
test.graphics.drawRect(0, 0, 100, 50);
addChild(test);

test.x = test.y = 50;

//Reflection
//test - object that will be reflected
//40 - alpha, 40%
//200 - ratio, max value 0xFF or 255, with 0xFF you will see full object in reflection
//-1 - update frame rate, for animated objects
//0 - some dropoff... actually I don't remember it, don't use it by passing zero, It affects reflection bounds
//10 - distance to the reflection in pixels...
var vars:ReflectVars = new ReflectVars(test, 40, 200, -1, 0, 10);
var reflect:Reflect = new Reflect(vars);

结果:

Reflection

如果你对这门课感兴趣,我可以把它移到GitHub。

答案 1 :(得分:1)

flipMatrix.translate(0, picture.height);

尝试将此更改为

flipMatrix.translate(0, picture.height + 20);

OR

 reflectionHolder.y=img.y+img.height;

尝试将此更改为

   reflectionHolder.y=img.y+img.height + 20;

用于在反射和放大之间增加空间真实的图片