Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

BitmapData - draw method of Actionscript 3.0

This is a discussion on BitmapData - draw method of Actionscript 3.0 within the Tutorials forums, part of the Flash English category; The BitmapData class is a wonderful world. It allows us to work images, animate them, play with their pixels and ...


Go Back   Forum Flash CS3 Flash CS4 > Flash CS3 Flash CS4 > Flash English > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 09-10-07, 19:43
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
BitmapData - draw method of Actionscript 3.0

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:
Attached Files
File Type: zip bitmapdata_draw.zip (11.1 KB, 29 views)

__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !

Last edited by Flep; 28-08-08 at 06:52..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Actionscript 3.0 perlinNoise method Flep Tutorials 6 05-12-08 10:05
New HitTest method of Actionscript 3.0 Flep Tutorials 3 05-03-08 23:06
curveTo Method with Actionscript 3.0 Flep Tutorials 0 29-09-07 10:18
BitmapData e metodo draw Flep Articoli e tutorials 0 19-09-07 14:13
BitmapData.draw() da movieclip aguia Actionscript 3.0 avanzato 7 31-08-07 07:24


All times are GMT. The time now is 13:25.

Powered by vBulletin version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap