1. Code
  2. ActionScript

Dynamic Reflection Generator Class for AS3

Scroll to top
Read Time: 2 min

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 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 :)

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.