Tutorial Details
- Program: Flash
Twice a month, we revisit some of our readers’ favorite posts from throughout the history of Activetuts+. This tutorial was first published in May, 2009.
Using ActionScript 3.0, we’ll create a cool Animated Background that we can use in Menu Screens or as a Music Visualizer.
Final Result Preview
Let’s take a look at what we’ll be working towards:
Step 1: Brief Overview
We’ll create a Class that will take care of the whole effect. When you call the class, you’ll be able to change the number of lights, color, alpha, size, direction, speed and the quality of the Blur filter. In this way it’s possible to achieve many different results when using it. Also, you can use any background you want.
Step 2: The .fla Document
Create a new ActionScript 3.0 document (File > New…). Set the Stage size to your desired dimensions, I’ve used 600 x 300 px.

Step 3: Your Background
Add or draw an image to use as the background, I’ve used a simple blue gradient (#02C7FB, #1F63B4).

Step 4: Layers
Rename the first layer to “Background” then create another one and name it “Code”. You can lock the “Code” layer to avoid placement of unwanted drawings in there.

Step 5: ActionScript
Save your work and let’s get into the code!
I’m assuming you already have a basic understanding of ActionScript 3.0, if you have any keyword specific doubts please refer to the Flash Help (F1).
Create a new ActionScript file (File > New…).

Step 6: Importing Required Classes
package Classes
{
/* Import required classes */
import flash.display.MovieClip;
import flash.filters.BitmapFilter;
import flash.filters.BlurFilter;
import flash.events.Event;
Here we import the Classes that we’ll use, remember that the word next to “package” is the name of the folder where our Class is located.
Step 7: Extending the Class
//We need to extend the class so we can use the addChild() method.
public class Lights extends MovieClip
{
Extending the MovieClip class will allow our class to inherit all of the methods, properties and functions that the MovieClip has, in this case we use it to get access to the addChild() method.
Step 8: The Variables
Although they have pretty much self explanatory names I’ll add short comments on each var to make sure it’s clear. They get their values from the parameters of the Main function.
private var lightsNumber:int; //The number of Lights on Stage
private var xSpeed:int; //The horizontal speed the Lights have
private var ySpeed:int; //Vertical Speed
private var lightsDir:String; //Direction the Lights are going, this can be Up, Down, Left or Right
private var areaW:int; //The width of the area where the effect will take place
private var areaH:int; //Height
private var lights:Array = new Array(); //This array will store all the Lights MovieClips so we can use them outside the For where we are going to create them.
Step 9: Main Function
/* Main function */
public function init(areaWidth:int, areaHeight:int, numberOfLights:int, lightSize:int, lightColor:uint, minimumScale:Number, hSpeed:int, vSpeed:int, dir:String, quality:int):void
{
areaW = areaWidth;
areaH = areaHeight;
lightsNumber = numberOfLights;
lightsDir = dir;
This is the function that we need to start the effect, we also set the variable values in this code.
Step 10: Creating the Lights
for (var i:int = 0; i < numberOfLights; i++)
{
/* Create the specified number of lights */
var light:MovieClip = new MovieClip();
/* Set random speed to x and y based on the params */
xSpeed = Math.floor((Math.random() * (hSpeed - -hSpeed + 1)) + -hSpeed);
ySpeed = Math.round((Math.random() * vSpeed) + 0.5);
light.xSpeed = xSpeed;
light.ySpeed = ySpeed;
/* Create lights */
light.graphics.beginFill(lightColor);
light.graphics.drawCircle(0, 0, lightSize / 2);
light.graphics.endFill();
In this code we use a “For” statement to create the number of Lights the user sets in the parameter and calculate a semi-random speed for every Light.
Step 11: Position
This sets a random start position for the Lights based on the Area parameters.
light.x = Math.floor(Math.random() * areaWidth); light.y = Math.floor(Math.random() * areaHeight);
Step 12: Blur Filter
var b:int = Math.floor(Math.random() * 10) + 5; //Calculates a random Blur between 0-9 and adds 5 to the result. var blur:BitmapFilter = new BlurFilter(b,b,quality); //The BlurFilter object. var filterArray:Array = new Array(blur); //To apply the filter we need an array of filters. light.filters = filterArray; //Sets the filters array to the Light
The above code applies the Blur. Remember that this code is still part of the For, so the Lights get different Blurs.
Step 13: Alpha
light.alpha = Math.random() * 0.6 + 0.1;
An easy one; this sets the alpha property between 0.1 and 0.6.
Step 14: Scale
light.scaleX = Math.round(((Math.random() * (1-minimumScale)) + minimumScale) * 100) / 100; light.scaleY = light.scaleX;
This sets the scale of the Lights between the original size and the minimum scale parameter. We use a round method to save processor.
Step 15: Adding the Lights to the Stage
addChild(light); /* Store lights in an array to use it later */ lights.push(light); /* Check for lights direction */ checkDirection();
This adds the Lights to the Stage, then we save them in an Array to use them later. It also the checkDirection function to see in which direction the Lights will go.
Step 16: Check Direction Function
private function checkDirection():void
{
for (var i:int = 0; i < lights.length; i++)
{
switch ( lightsDir )
{
case "up" :
lights[i].addEventListener(Event.ENTER_FRAME, moveUp);
break;
case "down" :
lights[i].addEventListener(Event.ENTER_FRAME, moveDown);
break;
case "right" :
lights[i].addEventListener(Event.ENTER_FRAME, moveRight);
break;
case "left" :
lights[i].addEventListener(Event.ENTER_FRAME, moveLeft);
break;
default :
trace("Invalid Direction!");
}
}
}
Here we use a for to get access to all the Lights in the Array, then check the direction variable to determine where to move the Lights. Depending on the direction we add the listener to the corresponding function.
Step 17: Move Functions
This next section may seem a little complicated, but it’s easier than you think. Basically, it moves the Light depending on the speed calculated in the Main function, then checks if the Light has “left” the Area of the effect. If that is the case, it sets a random position contrary to the direction the Light is heading.
private function moveUp(e:Event):void
{
e.target.x += e.target.xSpeed;
e.target.y-=e.target.ySpeed;
/* Reset light position, Y first, then X */
if (e.target.y + (e.target.height / 2) < 0)
{
e.target.y = areaH + (e.target.height / 2);
e.target.x=Math.floor(Math.random()*areaW);
}
if ((e.target.x + e.target.width / 2) < 0 || (e.target.x - e.target.width / 2) > areaW)
{
e.target.y = areaH + (e.target.height / 2);
e.target.x=Math.floor(Math.random()*areaW);
}
}
/* Move Down function */
private function moveDown(e:Event):void
{
e.target.x+=e.target.xSpeed;
e.target.y+=e.target.ySpeed;
/* Reset light position, Y first, then X */
if (e.target.y - (e.target.height / 2) > areaH)
{
e.target.y = 0 - (e.target.height / 2);
e.target.x=Math.floor(Math.random()*areaW);
}
if ((e.target.x + e.target.width / 2) < 0 || (e.target.x - e.target.width / 2) > areaW)
{
e.target.y = areaH + (e.target.height / 2);
e.target.x=Math.floor(Math.random()*areaW);
}
}
/* Move Right function */
private function moveRight(e:Event):void
{
e.target.x+=e.target.ySpeed;
e.target.y+=e.target.xSpeed;
/* Reset light position, Y first, then X */
if (e.target.y - (e.target.height / 2) > areaH || e.target.y + (e.target.height / 2) < 0)
{
e.target.x = 0 - (e.target.height / 2);
e.target.y = Math.floor(Math.random()*areaH);
}
if ((e.target.x - e.target.width / 2) > areaW)
{
e.target.x = 0 - (e.target.height / 2);
e.target.y = Math.floor(Math.random()*areaW);
}
}
/* Move Left function */
private function moveLeft(e:Event):void
{
e.target.x-=e.target.ySpeed;
e.target.y-=e.target.xSpeed;
/* Reset light position, Y first, then X */
if (e.target.y - (e.target.height / 2) > areaH || e.target.y + (e.target.height / 2) < 0)
{
e.target.x = areaW + (e.target.width / 2);
e.target.y=Math.floor(Math.random()*areaH);
}
if ((e.target.x + e.target.width / 2) < 0)
{
e.target.x = areaW + (e.target.width / 2);
e.target.y=Math.floor(Math.random()*areaW);
}
}
}
}
Step 18: Calling the Function
Save your class (remember that the file has to have the same name as the Class) and let’s get back to the Fla. Open the Actions Panel and write:
import Classes.Lights; var light:Lights = new Lights(); light.init(600, 300, 40, 60, 0xFFFFFF, 0.3, 3, 3, "up", 2); addChild(light);
That’s it! Test your movie to see your nice animated background!
Conclusion
Playing with the parameters in the class will result in a lot of different effects, try changing the background, set slower or faster speeds, use more Lights, less Blur and so on!
I hope you learned something you can use, thanks for reading!

Nice one !!!
Just a question : is it possible to place an element over the animation ?
thx u
JR-
thanx for the good tutorial =)
pangit
pangit ugly (Filipino)
Though I disagree :)
same here,
the effect was nice :)
çok teşekkürler
Top notch effect, how would I alter it to produce the effect at a certain x position & end at another x position, to eliminate the lights falling off the area width i.e. a smooth transition…..
Hi.. im wondering if this file can be used in SE C510 mobile? i’ve tryed to do this but i cant! :(((… plz give me advise….
Awesome, I’ve got to get started on how to code AS3.. I’m able to code Pascal, VB.net, C#.net, some C stuff, php and AS2 of course. which one of those, does AS3 resembles ?? from what I’ve seen it’s similar in some instructions to C# , but i just don’t know (:
peace & thanks for this great tutorial
Yep! It’s pretty similar to C#, Java, that kind of thing.
If I use this effect as a splash page with some text, can I add a link to the code?
thanks..awesome.
I am getting syntax errors. Any ideas of how to solve this problem??????
Pls Help.
Great tutorial! How can you change the colors of the lights?
Nevermind, got it! :)
Nice but it can be achieved with much less code, check my version at http://play.blog2t.net/flash-as3-bokeh/
Very nice work…
Very Nice and Simple tutorial ;)
But.. is ther any way of preventing de “gaging” (the small jumps on the bubbles) that appear in IE ?
this start to append after the second minute of animation.. :S
Best regards
Great tutorial!
I am thinking it’s possible to give this lights a few colors.
Good tutorial. I have been looking on how to produce this type of effect and glad I ran into this blog. I will be visiting often to learn new tricks in Flash.
Great effect! This would be great for a blog, if you make the bubbles slower!
Can’t you just make the light a seperate class, have a setDirection(_xspeed:Number, _yspeed:Number) in it and then in just one enter frame loop put . light[i].Move();
The move function would then be, ofcourse, Move():void { x += xspeed, y+= yspeed};
Way easier right?
Thanks for this great tutorial! I am new to the Flash, but in this tutorial, by following step by step, I create almost the same what’s in the preview. In short, it means – nice! =)
By the way, didn’t knew that ActionScript is so similar to, for example, PHP and JavaScript. =)
Btw, here is outcome: http://non.dagrevis.lv/junk/bokehs/ ! =]
Hi Carlos,
I’m confused by this line:
xSpeed = Math.floor((Math.random() * (hSpeed – -hSpeed + 1)) + -hSpeed);
Can you explain what the math does here?
Thanks!
I loved this tutorial and it inspired me to create a Silverlight version of the same, simple effect – you can find the details over here;
http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2011/01/24/create-a-whimsical-animated-silverlight-background.aspx
Mike.
Thanks a lot
keep it on
A real novice but i am unable to change the amount of lights and after 1 minute the screen just overfills with the circles making everything barely visible
Cool tutorial. Just starting to learn flash and AS3. Is there a way to make the “circles” collide with an object (example a static circle or text)?
Cool! Can be use as fixed background for HTML pages. See temporary example here: MCHAL.110mb.com. NOT my creation. Borrowed over the web.
Greetings.
love this tutorial alooot!!! really useful =)
but i have a QUESTION:
this picture explains everything: ” http://i300.photobucket.com/albums/nn37/themahen/problemo.jpg ”
all i know is, it have to do something with the code…but im not good at all sooo if ANYONE tell how to resolve this prob of mine, i would really appreciate it tooo the max!!!
just in-case u dont understand what the picture means:
I can’t click the button when the “Light” is on top of the button, is there anyway to make the “Light” appear behind the button layer??
~you can email me aso~
thanks in advance =)
no reply??
Tried this? http://pixelfumes.blogspot.com/2008/01/clicking-through-movieclips-to.html
OMG thanks aloooooot, u really saved my life…!!!
all i need is to add this script to the main timeline:
light.mouseEnabled = false;
light.mouseChildren = false;
and again thanks to Michael James Williams =)
You’re welcome :) Thanks for posting your solution.
i cant seem to get the part where i would open it in Fla….and add the pseudo code int he actions panel
thank you very much. This is really awesome design.
how to add multiple colors in the circle?
tanx a lot.
how about if you’re gonna use it to a large image? for example 1600×1280, TIA