Dynamic Reflection Generator Class for AS3

Hot on the heels of our Perfect Alpha Reflections Quick Tip comes this excellent utility class for dynamically reflecting your display objects, courtesy of Martin Christov.

How do I use it?

Step 1:

Copy the ReflectEffect.as file into the classpath of your Flash project (or the same directory as your FLA if you’re not using classpaths).

Step 2:

In your code, create a new instance of the ReflectEffect class, passing it the display object that you want it to reflect, like so:

var reflect:ReflectEffect = new ReflectEffect(movieClipToReflect);

Step 3:

Create a new variable for your reflected object and add it to the stage (or wherever you like):

var reflectedDown:Bitmap = reflect.down(length, fade, offset, blur, alpha);
reflectedDown.y = movieClipToReflect.y + movieClipToReflect.height;
addChild( reflectedDown );

The functions reflect.down(), reflect.up(), reflect.left() and reflect.right() will return a Bitmap of your object reflected in that direction. (Only the “length” parameter is required, the rest are optional.) As you see, you’ve got a few interesting options.

I think “length” is clear. So let’s take a look at “fade”. For example, if you put 40 for length and 30 for fade, the reflection is going to end 10px behind the actual reflection. Or, if you’d like you can set 40 for length and 50 for fade. This will make the reflection go from an alpha of 1.0 to something like 0.2.

The next is very interesting: “offset”. This basically moves your reflection up/down (or left/right) inside the Bitmap output. I use this option in the preview so that you can move your object around and see a Mac-like effect.

The last two parameters, “blur” and “alpha”, are pretty clear — take a look at the comments in the class for more info!

Enjoy, and let us know how you use this class :)

Add Comment

Discussion 13 Comments

  1. gm says:

    Hey, it’s dramatically slow!
    Optimize the code.
    Why not use PixelBender?

  2. Bruno Crociquia says:

    Nice class, thanks for sharing, just one thing,when i drag the label near a corner the frame rate drops instantly making it very laggy…Is it just me? Tested on Chrome and Firefox. I’m in a quad core and don’t usually experience frame drops… but my graphic card has been acting funny lately.

  3. stryju says:

    needs A LOT of speed up…

    • stryju says:

      tips:

      - why not to create (if blur is a must…) one blur instance and then apply the fade by manipulation sprites’ graphics ( beginGradientFill() ) ?
      - is the blur really a must? as u can see – it slows it a LOT…

  4. Author

    So slow?! I see some lagging only if you drag it to some of the corners and you have two reflections.

    stryju, blur is optional. you can set it off when instancing the reflection. Anyway, I don’t think this is a problem.

    • Karl Macklin says:

      I’ll have to agree with the others – I see a 45% CPU usage in Firefox (going from 0-1% when there’s no flash showing), when draggig the logo to any (single) edge.

      Definitely needs optimization!

  5. Author

    Well then at this point it is no good for using it the way I have for the preview.

    However, if you are instancing a reflection only once (not on mousemove as my app does) it will do you a perfect job.

    After all – it’s a freebie. Don’t use it if you don’t like it. :)

    I’ll take a look at ways of optimizing it when I have some spare time…

    Regards,
    Martin

  6. _mark says:

    Lag. Better solutions exist.

  7. joene says:

    pfff, why is everything for CS4, can’t open it on CS3

  8. Ngộ Độc says:

    haha ! NEED MORE SPEED

    it’s very lag when i draging it near mirror

  9. Jeremy says:

    No lag problem for my two years old Mackbook Pro, thanks for this helpful tip Martin!!

  10. surf says:

    martin….some good work… just that… u need to ave a look at your code again,
    if statements dont have any open or closed brackets,
    variables instances needs classification to demonstrate wether its an integer, number , object….
    its free but dat dont mean u doin us a favour..

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.