The BitmapData class is a wonderful world. It allows us to work images, animate them, play with their pixels and much more.
For those who haven"t yet approached this class, I"ll try to make life easier starting from the draw method of the BitmapData.
What does this method do" In a few words, you could say that this method shoots a photo to the object passed in as parameter ( source ).
This method takes 6 parameters"
but in this tutorial I"ll only use 1 to simplify the approach to the class.
The parameter in question is, in fact, the source, hence the name of the instance of the object I want to " photograph ".
Let"s enter the core:
I create an FLA and save it as " bitmapdata_draw.fla " inside of which I create a MovieClip and I animate it using the frame-to-frame technique.
I create the Document Class, as AS file that I save as " Drawing.as ".
I associate the FLA to the class ( we"ve already seen how in this site"s article "The best way to initiate the Main Class").
And here is how I"ve written the Document Class:
Code:
package
{
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.*;
public class Drawing extends MovieClip
{
private var bitmap:BitmapData;
private var bitmaps_array:Array;
private var W:int=100;
private var H:int=100;
private var ratio:int=5;
private var clipCounter:int=0;
private var distance:int=10;
private var lines:int=0;
public function Drawing()
{
stage.frameRate=31;
clip_mc.stop();
init();
}
private function init():void
{
bitmaps_array=new Array();
clip_mc.play();
addEventListener(Event.ENTER_FRAME,onEnterFrameDraw);
}
private function onEnterFrameDraw(event:Event):void
{
if(!(clip_mc.currentFrame%ratio))
{
bitmap=new BitmapData(W,H,true,0xFFFFFFFF);
bitmap.draw(clip_mc);
bitmaps_array.push(bitmap);
var image:Bitmap=new Bitmap(bitmap);
addChild(image);
image.y=150+distance;
image.x=(W+distance)*clipCounter;
if(!(clipCounter%ratio)&&clipCounter!=0)
lines++;
image.y+=(100+distance)*lines;
image.x=(W+distance)*(clipCounter-(ratio*lines));
clipCounter++;
}
if(clip_mc.currentFrame==clip_mc.totalFrames)
{
removeEventListener(Event.ENTER_FRAME,onEnterFrameDraw);
initButtonListener();
}
}
private function initButtonListener():void
{
ripeti_btn.addEventListener(MouseEvent.MOUSE_DOWN,clearData);
}
private function removeButtonListener():void
{
ripeti_btn.removeEventListener(MouseEvent.MOUSE_DOWN,clearData);
}
private function clearData(event:MouseEvent):void
{
for(var i:int=0;i < bitmaps_array.length;i++)
{
bitmaps_array[i].dispose();
}
removeButtonListener();
clip_mc.gotoAndStop(1);
clipCounter=0;
lines=0;
init();
}
}
}
This is the result:
In the script, under the onEnterFrame interval, I control the current frame"s number of the MovieClip we have in the FLA.
If that number is a multiple of 5, than I create a new BitmapData which I assign to new Bitmap(BitmapData) and I perform addChild to make it visible.
The rest of the code is needed to position the created images and manage the events of the " Ripeti " button.
Source files:
Bookmarks