Tuesday, August 25, 2009

AS3: Extending Bitmap class

To extend the Bitmap class and get it to display stuff properly, we need to call the constructor of the Bitmap class in our custom class using super(). When we call Super we need to pass a BitmapData var that contains the image we want to display. This has to happen before you do addChild(your_extended_bitmap_class)

So the custom class which would need to reside in a custom_class.as file would look like this:

package {
import flash.display.Bitmap;
import flash.display.BitmapData;

public class custom_class extends Bitmap {
public var our_new_property:String = "Blah";

// We will call super in our constructor.
public function custom_class(image:BitmapData = null) {
super(image);
}
}
}

To use this class, the code would look like this:

// Embed a image to use
[Embed(source="my_image.png")]
[Bindable]
private var image:Class;

private var tmp_image:Bitmap = new image();

private var custom_image:custom_class =
new custom_class(tmp_image.bitmapData);

// Now our custom properties and all
// the Bitmap properties are accessible

custom_image.our_new_property = "some string value";
custom_image.x = some x value;
stage.addChild(custom_image); // Will now get displayed on the stage.

No comments:

Post a Comment