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:
1 |
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):
1 |
var reflectedDown:Bitmap = reflect.down(length, fade, offset, blur, alpha); |
2 |
reflectedDown.y = movieClipToReflect.y + movieClipToReflect.height; |
3 |
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 :)