Advertisement
  1. Code
  2. Effects

Create a Whimsical Animated Flash Background

Scroll to top
Read Time: 8 min

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

1
package Classes
2
    {
3
/* Import required classes */
4
5
    import flash.display.MovieClip;
6
    import flash.filters.BitmapFilter;
7
    import flash.filters.BlurFilter;
8
    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

1
//We need to extend the class so we can use the addChild() method. 

2
3
  public class Lights extends MovieClip
4
{

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.

1
2
    private var lightsNumber:int; //The number of Lights on Stage

3
    private var xSpeed:int; //The horizontal speed the Lights have

4
    private var ySpeed:int; //Vertical Speed

5
    private var lightsDir:String; //Direction the Lights are going, this can be Up, Down, Left or Right

6
    private var areaW:int; //The width of the area where the effect will take place

7
    private var areaH:int; //Height

8
    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

1
/* Main function */
2
3
   	public function init(areaWidth:int, areaHeight:int, numberOfLights:int, lightSize:int, lightColor:uint, minimumScale:Number, hSpeed:int, vSpeed:int, dir:String, quality:int):void
4
    {
5
6
  	areaW = areaWidth;
7
	areaH = areaHeight;
8
	lightsNumber = numberOfLights;
9
	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

1
for (var i:int = 0; i < numberOfLights; i++)
2
{
3
/* Create the specified number of lights */
4
5
   var light:MovieClip = new MovieClip();
6
7
   /* Set random speed to x and y based on the params */
8
9
   xSpeed = Math.floor((Math.random() * (hSpeed - -hSpeed + 1)) + -hSpeed);
10
    ySpeed = Math.round((Math.random() * vSpeed) + 0.5);
11
12
   light.xSpeed = xSpeed;
13
    light.ySpeed = ySpeed;
14
15
16
    /* Create lights */
17
18
    light.graphics.beginFill(lightColor);
19
    light.graphics.drawCircle(0, 0, lightSize / 2);
20
    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.

1
light.x = Math.floor(Math.random() * areaWidth);
2
light.y = Math.floor(Math.random() * areaHeight);

Step 12: Blur Filter

1
var b:int = Math.floor(Math.random() * 10) + 5; //Calculates a random Blur between 0-9 and adds 5 to the result.

2
3
   var blur:BitmapFilter = new BlurFilter(b,b,quality); //The BlurFilter object.

4
5
   var filterArray:Array = new Array(blur); //To apply the filter we need an array of filters.

6
7
   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

1
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

1
light.scaleX = Math.round(((Math.random() * (1-minimumScale)) + minimumScale) * 100) / 100;
2
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

1
2
   addChild(light);
3
4
   /* Store lights in an array to use it later */
5
6
   lights.push(light);
7
8
   /* Check for lights direction */
9
10
   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

1
private function checkDirection():void
2
{
3
for (var i:int = 0; i < lights.length; i++)
4
{
5
switch ( lightsDir )
6
{
7
case "up" :
8
9
   lights[i].addEventListener(Event.ENTER_FRAME, moveUp);
10
11
   break;
12
    case "down" :
13
14
   lights[i].addEventListener(Event.ENTER_FRAME, moveDown);
15
16
   break;
17
    case "right" :
18
19
   lights[i].addEventListener(Event.ENTER_FRAME, moveRight);
20
21
   break;
22
    case "left" :
23
24
   lights[i].addEventListener(Event.ENTER_FRAME, moveLeft);
25
26
   break;
27
    default :
28
    trace("Invalid Direction!");
29
    }
30
    }
31
    }

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.

1
private function moveUp(e:Event):void
2
  {
3
  e.target.x += e.target.xSpeed;
4
  e.target.y-=e.target.ySpeed;
5
   /* Reset light position, Y first, then X  */
6
7
   if (e.target.y + (e.target.height / 2) < 0)
8
    {
9
    e.target.y = areaH + (e.target.height / 2);
10
    e.target.x=Math.floor(Math.random()*areaW);
11
    }
12
   if ((e.target.x + e.target.width / 2) < 0 || (e.target.x - e.target.width / 2) > areaW)
13
    {
14
    e.target.y = areaH + (e.target.height / 2);
15
    e.target.x=Math.floor(Math.random()*areaW);
16
    }
17
    }
18
   /* Move Down function */
19
20
   private function moveDown(e:Event):void
21
    {
22
    e.target.x+=e.target.xSpeed;
23
    e.target.y+=e.target.ySpeed;
24
   /* Reset light position, Y first, then X */
25
26
   if (e.target.y - (e.target.height / 2) > areaH)
27
    {
28
    e.target.y = 0 - (e.target.height / 2);
29
    e.target.x=Math.floor(Math.random()*areaW);
30
    }
31
   if ((e.target.x + e.target.width / 2) < 0 || (e.target.x - e.target.width / 2) > areaW)
32
    {
33
    e.target.y = areaH + (e.target.height / 2);
34
    e.target.x=Math.floor(Math.random()*areaW);
35
    }
36
    }
37
38
  /* Move Right function */
39
   private function moveRight(e:Event):void
40
    {
41
    e.target.x+=e.target.ySpeed;
42
    e.target.y+=e.target.xSpeed;
43
44
   /* Reset light position, Y first, then X */
45
   if (e.target.y - (e.target.height / 2) > areaH || e.target.y + (e.target.height / 2) < 0)
46
    {
47
    e.target.x = 0 - (e.target.height / 2);
48
    e.target.y = Math.floor(Math.random()*areaH);
49
    }
50
51
   if ((e.target.x - e.target.width / 2) > areaW)
52
    {
53
    e.target.x = 0 - (e.target.height / 2);
54
    e.target.y = Math.floor(Math.random()*areaW);
55
    }
56
    }
57
   /* Move Left function */
58
59
   private function moveLeft(e:Event):void
60
    {
61
    e.target.x-=e.target.ySpeed;
62
    e.target.y-=e.target.xSpeed;
63
   /* Reset light position, Y first, then X */
64
65
   if (e.target.y - (e.target.height / 2) > areaH || e.target.y + (e.target.height / 2) < 0)
66
    {
67
    e.target.x = areaW + (e.target.width / 2);
68
    e.target.y=Math.floor(Math.random()*areaH);
69
    }
70
71
   if ((e.target.x + e.target.width / 2) < 0)
72
    {
73
    e.target.x = areaW + (e.target.width / 2);
74
    e.target.y=Math.floor(Math.random()*areaW);
75
    }
76
    }
77
    }
78
    }

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:

1
import Classes.Lights;
2
  
3
  var light:Lights = new Lights();
4
  
5
  light.init(600, 300, 40, 60, 0xFFFFFF, 0.3, 3, 3, "up", 2);
6
  
7
  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!

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.