Let’s see a simple example of what we have so far:


The reflection follows perfectly. The problem we have here is that we redraw constantly the target object but the target object does not change. Only its scale or rotation property changes so we should be able to save resources by turning off the redrawing. We’ll create a setter and getter for an update property with a value of true or false:

public function get update():Boolean{
return updating;
}
public function set update(T:Boolean):void{
updating = T;
if(T){
redrawReflection();
}
}

Updating is our new boolean member variable. We simply set it to true or false. If it’s false we won’t constantly redraw the target object. We’ll leave it to true by default. Now in our code:

if(!updating && bitmap.bitmapData != null){

We simply check if updating is false meaning we don’t want to redraw the target object. We also check to see if we already drew something in which case the bitmap.bitmapData won’t be null. So if updating is false and bitmap.bitmapData is not null then we skip most of our drawing code. On the other hand if updating is true or bitmap.bitmapData is null then we draw. Here is another example:


Another example with update set to true. This time we draw constantly the target object since the target object has moving objects (the wheels):


Our class is ready! You can download all files here.

A few key points:

The stage.invalidate() and RENDER event allow us to run a display related code only once for each screen update. This saves CPU resources so don’t forget to include this design in all your display related class.

Our class does not provide an auto update drawing (Timer driven or else). This is because I always prefer to let the programmer choose how he/she wants to manage the CPU resources.

The key classes we used are the BitampData, Matrix and Rectangle.


Don’t hesitate to leave a comment!