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:
The result: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); } } } }
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 !
Bookmarks