<< Chapter < Page | Chapter >> Page > |
Will explain in fragments
I will explain the ActionScript code for the program named ChromaKey01 in fragments. A complete listing of the code for the ActionScript class named Driver is provided in Listing 7.
The processChromaKey method
This program illustrates the use of a custom method named processChromaKey . This method scans all of the pixels in an incoming bitmap and identifies those pixels with a color of pure magenta. The alphavalues for the magenta pixels are set to zero to make them transparent. Then the bitmap is modified accordingly.
The method can easily be modified to accommodate an image background of any solid color such as green or blue. Note however, that magenta is commonly usedin computer graphics, and is often referred to as "magic pink."
Beginning of the ActionScript class named Driver
The Flex code in Listing 8 instantiates a new object of the class named Driver and adds that object to the display list as a child of the Application container.
The ActionScript code for the class named Driver begins in Listing 1.
package CustomClasses{
import flash.display.Bitmap;import flash.display.BitmapData;
import flash.geom.Rectangle;import flash.utils.ByteArray;
import mx.containers.Canvas;import mx.controls.Image;
import mx.events.FlexEvent;//====================================================//public class Driver extends Canvas {
private var origImage:Image = new Image();
Extend the Flex Canvas class
Note that the Driver class extends the Flex Canvas class, thus making it possible to add other objects as children of an object ofthe Driver class.
Another reason for extending the Canvas class
I could have accomplished that purpose by extending any number of Flex container classes. However, an important characteristic of the Canvas class for this application is the ability to specify the physical locations of the child objects in absolute coordinates. Few if anyother Flex containers allow you to do that,
The constructor
The constructor for the Driver class is shown in its entirety in Listing 2.
public function Driver(){//constructor
//Make this Canvas visible with a yellow background.setStyle("backgroundColor",0xFFFF00);
setStyle("backgroundAlpha",1.0);//Load the file mage and embed it in the swf file.
//Note the slash that is required by FlashDevelop.[Embed("/dancer.png")]
var img:Class;origImage.load(img);
//Register a CREATION_COMPLETE listenerthis.addEventListener(FlexEvent.CREATION_COMPLETE,
completeHandler);} //end constructor
Make the canvas visible
The constructor begins by setting two style properties on the Canvas object to make it yellow and opaque.
You learned how to set styles on objects in ActionScript code as early as the lesson titled What is OOP and Why Should I Care?
Load the image from the file
Then the constructor loads the image from the image file named dancer.png and embeds it in the swf file. You learned how to do this as early as the lesson titled Inheritance - The Big Picture.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?