Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

getPixel/setPixel methods of the BitmapData Class

This is a discussion on getPixel/setPixel methods of the BitmapData Class within the Tutorials forums, part of the Flash English category; The names of these two methods do not give space to misunderstandings... They are simply two methods of the BitmapData ...


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 (permalink)  
Old 29-09-07, 10:04
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
getPixel/setPixel methods of the BitmapData Class

The names of these two methods do not give space to misunderstandings...
They are simply two methods of the BitmapData Class that receive and assign a specific value RGB.

The getPixel method, wants 2 parameters:
- the coordinate x of the pixel from which we want to recover the value RGB
- the coordinate y of the pixel from which we want to recover the value RGB

The setPixel method, wants 3 parameters
- the coordinate x of the pixel to which we want to impost the value RGB
- the coordinate y of the pixel to which we want to impost the value RGB
- the value RGB used

Let's see how it works...

I create a FLA and save it as 'scambio.fla' inside which I import 2 images placed in 2 Movie Clip with the instance names 'pic_0_mc' and 'pic_1_mc'.
I create a Document Class, an AS file saved as 'Scambio.as', implemented the following way:
Code:
package
{
	import flash.display.MovieClip;
	import flash.display.BitmapData;
	import flash.display.Bitmap;
	import flash.events.Event;
	
	public class Scambio extends MovieClip
	{
		private var clip:MovieClip;
		
		private var immagine_1:BitmapData;
		private var immagine_2:BitmapData;
		private var copia:BitmapData;
		
		private var bitmap:Bitmap;
		
		private var startX:Number=0;
		private var startY:Number=0;
		private var startW:Number=0;
		private var startH:Number=1;
		
		public function Scambio()
		{
			init();
			crea();
			creaEffetto();
		}
		
		private function init():void
		{
			stage.frameRate=31;
		}
		
		private function crea():void
		{
			immagine_1=new BitmapData(pic_0_mc.width,pic_0_mc.height,true,0xFF000000);
			immagine_1.draw(pic_0_mc);
			immagine_2=new BitmapData(pic_0_mc.width,pic_0_mc.height,true,0xFF000000);
			immagine_2.draw(pic_1_mc);
			
			clip=new MovieClip();
			
			copia=new BitmapData(pic_0_mc.width,pic_0_mc.height,true,0xFFFFFFFF);
			bitmap=new Bitmap(copia);
			clip.addChild(bitmap);
			
			clip.x=stage.stageWidth/2-clip.width/2;
			clip.y=pic_0_mc.y+pic_0_mc.height+50;
			
			addChild(clip);
		}
		
		private function creaEffetto():void
		{
			clip.addEventListener(Event.ENTER_FRAME,effetto1);
			clip.addEventListener(Event.ENTER_FRAME,effetto2);
		}
		
		private function effetto1(e:Event):void
		{
			for(var i:Number=0;i < immagine_1.width;i++)
			{
				startX=i;
				bitmap.bitmapData.setPixel(startX,startY,immagine_1.getPixel(startX,startY));
			}
			startY+=2;
			if(startY>=immagine_1.height)
			{
				e.target.removeEventListener(Event.ENTER_FRAME,effetto1);
			}
		}
		
		private function effetto2(e:Event):void
		{
			for(var i:Number=0;i < immagine_2.width;i++)
			{
				startW=i;
				bitmap.bitmapData.setPixel(startW,startH,immagine_2.getPixel(startW,startH));
			}
			startH+=2;
			if(startH>=immagine_2.height)
			{
				e.target.removeEventListener(Event.ENTER_FRAME,effetto2);
			}
		}
	}
}
The result:







Let's analise the code.

Properties


a MovieClip instance into which I will placed the new image
private var clip:MovieClip;
three instances of BitmapData
the first one will take a picture (draw() method) of MovieClip pic_0_mc
the second one will take a picture (draw() method) of MovieClip pic_1_mc
the third one will be the BitmapData created
private var immagine_1:BitmapData;
private var immagine_2:BitmapData;
private var copia:BitmapData;
an instance Bitmap which will render the instance BitmapData 'copia' visible
private var bitmap:Bitmap;
four numerical variables used for the cycles
private var startX:Number=0;
private var startY:Number=0;
private var startW:Number=0;
private var startH:Number=1;

Methods

init();
I impost the frame rate
stage.frameRate=31;

crea();
I create two new BitmapData and use the draw() method to take a picture of the respective MovieClip placed on stage with the images inside
immagine_1=new BitmapData(pic_0_mc.width,pic_0_mc.height,true,0xF F000000);
immagine_1.draw(pic_0_mc);
immagine_2=new BitmapData(pic_0_mc.width,pic_0_mc.height,true,0xF F000000);
immagine_2.draw(pic_1_mc);
I create a new MovieClip
clip=new MovieClip();
I create the third BitmapData (using the same background color as the FLA)
copia=new BitmapData(pic_0_mc.width,pic_0_mc.height,true,0xF FFFFFFF);
I create a new Bitmap to which I pass the BitmapData 'copia'
bitmap=new Bitmap(copia);
I insert the Bitmap inside the MovieClip 'clip' otherwise it would not be visible
clip.addChild(bitmap);
I position 'clip'
clip.x=stage.stageWidth/2-clip.width/2;
clip.y=pic_0_mc.y+pic_0_mc.height+50;
I add to DisplayObject the MovieClip 'clip' otherwise it would not be visible
addChild(clip);

creaEffetto();
I add two listeners to the event ENTER_FRAME which will call two different functions 'effetto1' and 'effetto2'
clip.addEventListener(Event.ENTER_FRAME,effetto1);
clip.addEventListener(Event.ENTER_FRAME,effetto2);

effetto1();
I create a cycle starting from zero and which has the value of the width of immagine_1 as a maximum iteration, into which:
- I impost the numerical variable startX equal 'i'. This way, during the full cycle, 'i' will cover all the values from zero to the maximum width of immagine_1
- I tell the BitmapData 'copia' (which is now a 'bitmap' propertie) to use the setPixel method passing the values:
startX which goes along the width of immagine_1, startY which remains to zero (using these two values I tell Flash the coordinates of the pixel on which I want to work)
then, as a third parameter, the setPixel method wants a value RGB which I recover using getPixel on immagine_1 which itself wants 2 parameters, startX and startY, the same coordinates as the same pixel
for(var i:Number=0;i < immagine_1.width;i++)
{
startX=i;
bitmap.bitmapData.setPixel(startX,startY,immagine_ 1.getPixel(startX,startY));
}
I increase startY of 2 so as to jump a line (as in the pair lines I will put the pixels of immagine_2)
startY+=2;
I check if startY reaches the value of the immagine_1 width. If yes, the interval is stopped
if(startY>=immagine_1.height)
{
e.target.removeEventListener(Event.ENTER_FRAME,eff etto1);
}

effetto2();
same thing as effetto1(), just that I recover the values of immagine_2.

Interesting no? :)

Stay tuned !
__________________

 


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 !
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
Tutorial 3 - the methods Flep Object Oriented Programming - tutorials 6 27-07-08 02:21
Esportare una bitmapdata masky Actionscript 3.0 avanzato 6 02-04-08 08:21
Encapsulation, Getter and Setter methods Flep Tutorials 6 08-02-08 12:34
Tutorial 5 - attribute of the methods Flep Object Oriented Programming - tutorials 0 15-10-07 06:43
Metodi getPixel - setPixel della classe BitmapData Flep Articoli e tutorials 0 20-09-07 10:55


All times are GMT. The time now is 16:54.

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