The BitmapData class allows the creation and manipulation of bitmaps during runtime. This means you can create complex images without some of the performance problems associated with bitmaps on the timeline. You can also perform certain visual effects that would have been impossible in previous versions of Flash.

Another nice feature of the BitmapData class is that it can also retrieve information about bitmaps, as you will see in this next example:


Create a new Flash document.


Import an image 400x300 in size (or use the one from the website) by going to File, Import, Import to Library.


Open the library by going to Window, Library, and you will see the image you just imported. Select it, then right-click it (Mac:Ctrl+click) and select Linkage.


Check the Export for ActionScript check box, give it an identifier of bridge, and click OK.


On the stage, draw a rectangle 50x300 in size and place it at 400,0.


While the rectangle is selected, choose Modify, Convert to Symbol.


Give it a symbol name of recMC, set the behavior to Movie Clip, and then click OK.


Back on the main stage, give the rectangle an instance name of rec_mc.


Create a second layer called actions and place the following code in the first frame of that layer:

 import flash.display.BitmapData; //get the image we need var img:BitmapData = BitmapData.loadBitmap("bridge"); //create a movie clip to hold the image var target_mc:MovieClip = this.createEmptyMovieClip("target_mc",1); //attach the movie target_mc.attachBitmap(img,1); //set up the color object var pix_color:Color = new Color(rec_mc); //monitor mouse movement this.onMouseMove = function(){     //make sure the mouse is over the image     if(target_mc.hitTest(_xmouse,_ymouse)){         pix_color.setRGB(img.getPixel(_xmouse,_ymouse));     } } 

The preceding code does a lot of things. First, it imports the necessary class file. Then it creates an instance of the BitmapData object and loads the bitmap directly from the library. Next, the code creates an empty movie clip to hold the bitmap. After that, you instantiate a Color object, which you use inside the onMouseMove event. Then you create the event to trigger whenever the user moves the mouse, and you make sure that the mouse cursor is over the image. If the mouse is over the image, you pass the color of that pixel through the Color object you created to change the color of the rectangle.

Test the movie and you will see that the rectangle changes to the color of the pixel the mouse is hovering over, as shown in Figure 1.14.

Figure 1.14. The BitmapData is an object with a great deal of potential for image manipulation.

Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319 © 2008-2017.
If you may any questions please contact us: