We saw that one of the challenges is that the object we need to draw might be scaled or rotated or have some filters applied. Flash does not update the screen until all code is executed so we can take advantage of that. We can set the target object back to normal by removing all filters, rotation, scaling, then we can draw our reflection, then we reset the target object back with filters, scale, rotation. Let’s do that here:

private function getTargetProperty():void{
angle = TargetDisplayObject.rotation;
TargetDisplayObject.rotation = 0;
SCALEX = TargetDisplayObject.scaleX;
TargetDisplayObject.scaleX = 1;
SCALEY = TargetDisplayObject.scaleY;
TargetDisplayObject.scaleY = 1;
FILTERS = TargetDisplayObject.filters;
TargetDisplayObject.filters = [];

We store all these property into new member variables. Then of course once we are done drawing we add back the property:

private function setTargetProperty():void{
TargetDisplayObject.rotation = angle;
TargetDisplayObject.scaleX = SCALEX;
TargetDisplayObject.scaleY = SCALEY;
TargetDisplayObject.filters = FILTERS;

And in our drawing method we have this:

private function createReflection():void{
//we draw here

So when we are ready to draw, we draw the target object exactly as it is but the next step is to get the registration point.

private function getRegistration():void{
rect = TargetDisplayObject.getBounds(TargetDisplayObject.parent);
Xoffset = rect.x;
Yoffset = rect.y;

So we store the target object bounds in a new member variable rectangle instance, then we store in two number member variables the distance from the rectangle coordinates to the target coordinates. So now we can basically draw:

private function createReflection():void{
bmd.dispose();//clear the former bitmapdata
var temp:BitmapData = new BitmapData(rect.width, rect.height, true, 0xFFFFFFFF);
//we create a temporary bitmapdata
matrix = new Matrix();//start fresh with a clean matrix
matrix.translate(-Xoffset, -Yoffset); //this is here that we handle the registration!
temp.draw(TargetDisplayObject, matrix);//and we draw of course
matrix.translate(Xoffset, Yoffset); // now time to get back to normal registration
matrix.translate(0, -rect.height); //get ready to flip the drawing
matrix.scale(1,-1);//flip the matrix
bmd = new BitmapData(rect.width, rect.height, true, 0xFFFFFFFF); //our bitmapdata member variable
bmd.draw(temp, matrix);//and we draw the flipped bitmapdata
bitmap.bitmapData = bmd;//assign our new bitmapdata to our bitmap
temp.dispose();//clean up

Time to run a little test just to see how all this goes. I changed the registration of our MovieClip and assigned to it an instance of our new class:

The drawing is good but we did not catch up the registration yet. Also we should be able to set where the reflection should be set like right, left, bottom, top. This will change the way we flip the bitmapdata. Let’s see that in the next section.