Use BitmapData to make a fuzzy TV effect
In this tutorial you will learn some of the basics of bitmap data and how to do some cool stuff with it. I have written a tutorial for both Actionscript 2.0 and Actionscript 3.0. This will lag a lot with AS 2, so don’t make the bitmap image too big or you will crash flash (AS 3 doesn’t lag as much, but if you make the image more than 400×400 it will start to lag a lot).
Final Product:
Actionscript 2.0 version (Take this tutorial if you are using Flash 8 )
Actionscript 3.0 version (For Flash CS3 users)
First of all open a new flash document. Set it’s width and height to 100. Open the actions panel (F9) and enter this code:
var a:BitmapData = new BitmapData(100, 100);
attachBitmap(a,0);
onEnterFrame = function () {
for (i=0; i<100; i++) {
for (j=0; j<100; j++) {
a.setPixel(i,j,Math.random()*0xFFFFFF);
}
}
};
Test your movie and you should see something like above. Simple huh? If you have no idea what is going on don’t worry, I will explain.
The first line just imports the BitmapData class. If we didn’t do this line none of the other code would work.
var a:BitmapData = new BitmapData(100, 100);
attachBitmap(a,0);
First we create a new BitmapData object called ‘a’, setting it’s width and height to 100. Next we attach ‘a’ to the stage using the attachBitmap() function. The first parameter is the object you want to attach and the second is the depth (basically the same as attachMovie()). Objects on the stage (including movieclips) with a higher depth appear above objects with lower depths.
onEnterFrame = function () {
for (i=0; i<100; i++) {
for (j=0; j<100; j++) {
a.setPixel(i,j,Math.random()*0xFFFFFF);
}
}
};
This is where it all happens. Each frame this onEnterFrame function will run, executing the script inside it. It runs a for loop which will loop 100 times, and inside that for loop is another for loop which will run 100 times every time the first loop runs. So this code is being executed 10,000 times a frame (no wonder it lags).
We are using the function setPixel() to change each pixel inside this 100×100 bitmap data object to change each pixel (there is 10,000 of them, thats why the loop runs 10,000 times) to a random colour. Math.random()*0xFFFFFF will return a random colour from #000000 (black) to #FFFFFF (white). i is the _x coordinate of the pixel it is changing and j is the _y coordinate.
And thats it!
Open a new Flash Document (Actionscript 3.0) and set the stage height and width to 200×200. Open the actions panel (F9) and enter this code:
var b:Bitmap = new Bitmap(a);
addChild(b);
function frame(event:Event){
for (var i:uint = 0; i<200; i++) {
for (var j:uint = 0; j<200; j++) {
a.setPixel(i,j,Math.random()*0xFFFFFF);
}
}
}
stage.addEventListener(Event.ENTER_FRAME,frame);
Test your movie and you should see a result like at the top of the page.
First of all we create a bitmap data object with a width and height of 200 called ‘a’. We then create a new bitmap object with ‘a’. We need to do this so we can display ‘a’. We then add the display object (b) to the stage using addChild().
function frame(event:Event){
for (var i:uint = 0; i<200; i++) {
for (var j:uint = 0; j<200; j++) {
a.setPixel(i,j,Math.random()*0xFFFFFF);
}
}
}
Here is an Event listener that we will later add to the stage. It contains the code that makes the bitmap data object change color. It has 2 for loops inside it. The first one loops 200 times, and the other loops 200 times everytime the first one loops (40,000 times all up, which is one for each pixel). When every time the second loop loops we run the setPixel function to set a pixel in ‘a’ to a different colour. We use i and j as the X and Y coordinates for the pixel because then every pixel will be covered.
Math.random()*0xFFFFFF is used to get a random colour from #000000 (black) to #FFFFFF (white), which is every colour.
And thats all there is to it!
Download AS 2 source file (Flash 8 )
Download AS3 source file (Flash CS3)















yeah.. as 2 really lags
thanks mite look at doing this later!
Does this work in MX 2004?
Hi! I try to expand the area using:
var a:BitmapData = new BitmapData(450, 450); attachBitmap(a,0); onEnterFrame = function () { for (i=0; i<450; i++) { for (j=0; j<450; j++) { a.setPixel(i,j,Math.random()*0xFFFFFF); } } };
But now it´s so slow!! Please help !
that is 45000 operations per frame, so perhaps it’s your computer that is slow.
Why not use noise() or perlin_noise()? it should be faster than setting all the pixels individually…
i use flash mx 2004 and this dusnt work
cud u help me?
Hello
I can’t get this to work in MX 2004, and that’s the version I’m using. I have Flash 8 installed, but I prefare to use Flash MX.
How do i change it to 1440px by 1080 px? I have tryed bud seems to be totally differrent with any changes applyed to it…