Advertisement
  1. Code
  2. Coding Fundamentals
  3. Game Development

Use the Flash Project Panel‏ to Build a Dynamic AS3 Menu

Scroll to top
This post is part of a series called GreenSock Tweening Platform.
Create an Organic Dissolve With Perlin Noise
Creating a Virtual Joystick for Touch Devices

During this tutorial, we'll use the Project Panel in Flash to create a vertical animated AS3 menu. The whole process will allow you to easily customize all aspects of the menu using the parameterized constructors. Read on to learn more!


Final Result Preview

Let's take a look at the final result we will be working towards:


Step 1: Create a New Project

Start by creating a new project. Open Flash and go to File > New, then select Flash Project. The Project panel will appear.

simple Flash menu tutorialsimple Flash menu tutorialsimple Flash menu tutorial

In the Projects dropdown select New Project. Type the project name "AnimatedMenu". In the Root Folder, browse and choose where you want to save your project; you can select an already existing folder or create a new one. Make sure the ActionScript version is set to ActionScript 3.0 and click Create Project.

simple Flash menu tutorialsimple Flash menu tutorialsimple Flash menu tutorial

Step 2: Add the Classes Folder

Now that the project is created, we are going to add a new folder to group in it our classes. Still in the same panel "Project" press the "New Folder" icon in the bottom, name the new folder "Classes" and click Create Folder.

simple Flash menu tutorialsimple Flash menu tutorialsimple Flash menu tutorial

Step 3: Install TweenLite

During this tutorial we will use the TweenLite classes from GreenSock for tweening, so we need to add it to our project. Download it and extract it, place it in your project folder (so you will have AnimatedMenu/com/greensock/).

Now if you refresh the Project panel you should see this structure:

simple Flash menu tutorialsimple Flash menu tutorialsimple Flash menu tutorial

Step 4: Create a New Flash File

Click the "New File" icon in the Project panel to create a new file, name it "AnimatedMenu.fla" (make sure that the File Type is Flash File) and click Create File.

simple Flash menu tutorialsimple Flash menu tutorialsimple Flash menu tutorial

Set the stage size to 600x350px.

simple Flash menu tutorialsimple Flash menu tutorialsimple Flash menu tutorial

Step 5: Create a New ActionScript File

Select the Classes folder and click the "New File" icon, set the File Type to ActionScript, name it "Main". This will be our document class, if you are not familiar with document classes this Quick Tip on using a document class will help you.

simple Flash menu tutorialsimple Flash menu tutorialsimple Flash menu tutorial

Step 6: Set a Relative Source Path

This will allow us to use any class located in our Classes folder without the need to change the package name. Go to File > Publish Settings, select the Flash tab, then click ActionScript Settings. Click the plus button "Add New Path" and write the relative path './Classes'.

simple Flash menu tutorialsimple Flash menu tutorialsimple Flash menu tutorial

Step 7: Start Coding the Main.as File

Inside the package Classes, import the Sprite Class and use it to extand the class "Main". Here is the code :

1
2
package Classes 
3
{
4
	import flash.display.Sprite;
5
6
	public class Main extends Sprite
7
	{

Step 8: Declare the Variables

These are the variables we will use (MenuItem is an ActionScript class that we will create later)

1
2
private var item1:MenuItem;
3
private var item2:MenuItem;
4
private var item3:MenuItem;
5
private var item4:MenuItem;

Step 9: The Constructor

Now we are going to code the constructor, it contains the code that will be executed when this class is called.

1
2
public function Main():void
3
{

Step 10: Create Four Menu Items

Instantiate the MenuItem class to create four menu items with different colors, labels, functionalities and positions:

1
2
//Create four instances of the MenuItem class and spacify the parameters (x,y,color,label,URL).

3
item1 = new MenuItem(100,60,0x28D9E9,"Home page","https://code.tutsplus.com/categories/flash");
4
item2 = new MenuItem(140,150,0xA8FA2D,"Services","https://design.tutsplus.com");
5
item3 = new MenuItem(120,240,0xFC30FC,"About me","https://code.tutsplus.com");
6
item4 = new MenuItem(160,330,0xEE2B2B,"Contacts","https://design.tutsplus.com");

You can change the URLs to point to other sites.


Step 11: Add the Items to the Stage

This code simply adds the four items created earlier to the stage.

1
2
//Add the items to the stage.

3
addChild(item1);
4
addChild(item2);
5
addChild(item3);
6
addChild(item4);

Now we are done with the Main class, here is the full code of this class.

1
2
package Classes 
3
{
4
	import flash.display.Sprite;
5
6
	public class Main extends Sprite
7
	{
8
		private var item1:MenuItem;
9
		private var item2:MenuItem;
10
		private var item3:MenuItem;
11
		private var item4:MenuItem;
12
13
		public function Main():void
14
		{
15
			//Create four instances of the MenuItem class and spacify the parameters (x,y,color,label,URL).

16
			item1 = new MenuItem(100,60,0x28D9E9,"Home page","https://code.tutsplus.com/categories/flash");
17
			item2 = new MenuItem(140,150,0xA8FA2D,"Services","https://design.tutsplus.com");
18
			item3 = new MenuItem(120,240,0xFC30FC,"About me","https://code.tutsplus.com");
19
			item4 = new MenuItem(160,330,0xEE2B2B,"Contacts","https://design.tutsplus.com");
20
			
21
			//Add the items to the stage.

22
			addChild(item1);
23
			addChild(item2);
24
			addChild(item3);
25
			addChild(item4);
26
		}
27
	}
28
}

This class is too short to do all the functions that our menu is supposed to do, so we will create the "MenuItem.as" class that contains the necessary functions for our menu.


Step 12: Create the MenuItem.as

Add a new ActionScript 3 file in the Classes folder exactly like you did for the Main.as. Name it "MenuItem.as".

simple Flash menu tutorialsimple Flash menu tutorialsimple Flash menu tutorial

Step 13: Import Classes

These are the classes that we need to import for our new class. Don't forget you can always look them up in the LiveDocs.

1
2
package Classes 
3
{
4
	import flash.display.Sprite;
5
	import flash.events.Event;
6
	import flash.events.MouseEvent;
7
	import com.greensock.*;
8
	import com.greensock.TweenLite;
9
	import com.greensock.easing.*;
10
	import com.greensock.plugins.*;
11
	import flash.text.TextField;
12
	import flash.media.Sound;
13
	import flash.net.navigateToURL;
14
	import flash.net.URLRequest;

Step 14: Class and Variables

Declare the MenuItem class (should have the same name as its File name "MenuItem") and extend the Sprite class.

1
2
public class MenuItem extends Sprite
3
{

These are the variables that we need at the moment, later we will add some others as we progress in this tutorial.

1
2
private var rect1:DynamicMovie = new DynamicMovie(); //Use the DynamicMovie class instead of Sprite class. 

3
private var rect2:DynamicMovie = new DynamicMovie(); //This allow us to change the registration point.

4
private var rect3:DynamicMovie = new DynamicMovie(); //So we can rotate the rectangels around their centers.

5
private var X:Number;
6
private var Y:Number;
7
private var Color:uint;

DynamicMovie is an AS3 class based on an old AS2 class written by Darron Schall; this AS3 class extends MovieClip and adds a new set of properties (x2, y2, rotation2, scaleX2, scaleY2, mouseX2, mouseY2) that allow you to manipulate the sprite based on a contextual registration point that can be set using the setRegistration() method.

We need this class to rotate the rectangles around their centers. So let's place it.


Step 15: Add the DynamicMovie Class

You find this class in the source folder of this tutorial or you can download it from oscartrelles.com, then just place it in the Classes Folder so that it can be recognized by our code.

simple Flash menu tutorialsimple Flash menu tutorialsimple Flash menu tutorial

Step 16: The Constructor

This is the constructor of the MenuItem.

1
2
public function MenuItem(posX:Number, posY:Number,color:uint,Title:String,URL:String)
3
{
4
	//Get the position and color parameters. 

5
	X = posX;
6
	Y = posY;
7
	Color = color;
8
			
9
	// Call the addRect function to add 3 rectangles with the specified parameters.

10
	addRect(rect1,X-12,Y,360,62,Color,0.3,3);
11
	addRect(rect2,X-4,Y,360,62,Color,0.4,0);
12
	addRect(rect3,X,Y,360,62,Color,0.7,-2);
13
}

Step 17: addRect() Function

This function is responsible for drawing the rectangles according to the given parameters: position, width, height, color, alpha and rotation.

1
2
private function addRect(rect:DynamicMovie,X:Number, Y:Number, width:Number, height:Number,color:uint, alpha:Number, rotation:Number)
3
{
4
	rect.setRegistration(X+(width/2),Y+(height/2));
5
	rect.graphics.beginFill(color,alpha);
6
	rect.graphics.drawRect(X,Y,width,height);
7
	addChild(rect);
8
	rect.rotation2 = rotation;
9
}

Now you can test it and you will see this:

Sure, we can't call it a menu if we don't add some labels. We'll deal with that in the next step.


Step 18: Add a Dynamic TextField

Head back to your AnimatedMenu.fla file and add a new symbol (Ctrl + F8); name it "Text_mc" and select "Export for ActionScript".

simple Flash menu tutorialsimple Flash menu tutorialsimple Flash menu tutorial

Now inside this symbol add a 160x30px Dynamic TextField using the Text Tool (T). This is the font I used: Creampuff Regular, 24px, #FFFFFF. Name the instance "txtLabel".

Select the TextField and go to Window > Align (Ctrl + K) and hit the buttons "Align left edge" and "Align top edge" (make sure that the checkbox "Align to stage" is selected)

simple Flash menu tutorialsimple Flash menu tutorialsimple Flash menu tutorial

Step 19: Embed the Font

After creating the text field with the specified font we should embed it to display the text properly.

So go to Text > Font Embedding, give it a name (for example "Font1"), select the Creampuff font from the Family combo box, in the Character ranges select all of the Uppercase and the Lowercase, then press the "plus" button located in the left section. See the image below:

simple Flash menu tutorialsimple Flash menu tutorialsimple Flash menu tutorial

Step 20: Add Labels

To add labels to the menu items we are going to instantiate the Text_mc MovieClip. Add this line of code to the variables in the MenuItem.as file.

1
2
private var txt:Text_mc = new Text_mc();

Now we should assign to the TextField the title given in the constructor's parameters.

Add this code to the end of the constructor.

1
2
//Assign a title to the TextField and place it. 

3
txt.txtLabel.text = Title;
4
txt.x = X + 70;
5
txt.y = Y + 16;
6
addChild(txt);

This is what you should get:

In the next steps we will add some functions to animate the menu.


Step 21: Menu Button

To turn our menu item into a button we should add a transparent rectangle over it and set the buttonMode to true. So add this variable to the variable list.

1
2
private var menuButton:DynamicMovie = new DynamicMovie();

In the end of the constructor add this code:

1
2
//Use the addRect function to draw a transparent rectangle over the menu item.

3
addRect(menuButton,X-10,Y-5,380,80,Color,0,0);
4
buttonMode = true;

Step 22: Add Event Listeners

Add the following event listeners to the menuButton at the end of the constructor.

1
2
menuButton.addEventListener(MouseEvent.MOUSE_OVER,mouseOver);
3
menuButton.addEventListener(MouseEvent.MOUSE_OUT,mouseOut);
4
menuButton.addEventListener(MouseEvent.CLICK,mouseClick);

Step 23: Mouse Over

This function will be called when the mouse is over the menuButton.

1
2
private function mouseOver(e:MouseEvent)
3
{
4
	var timeline:TimelineLite = new TimelineLite();
5
	
6
	//Rotate the rectangels.

7
	new TweenLite(rect1,.3,{rotation2:-4});
8
	new TweenLite(rect2,.3,{rotation2:0});
9
	new TweenLite(rect3,.3,{rotation2:5});
10
11
	//Tween the text.

12
	timeline.append(new TweenLite(txt,.3,{x:X+45,scaleX:1.1,scaleY:1.1}));
13
	timeline.append(new TweenLite(txt,.3,{x:X+70,alpha:1}));
14
15
	//Add a Glow Filter to the text.;

16
	new TweenMax(txt,.3,{glowFilter:{color:0xffffff,alpha:1,blurX:5,blurY:5,strength:1,quality:3}});
17
}

Here we are using the GreenSock classes TimelineLite and TweenMax to animate the button. Search the Activetuts+ site for more tutorials featuring GreenSock.


Step 24: Mouse Out

When the mouse is out this function will return the menu to its initial position.

1
2
private function mouseOut(e:MouseEvent)
3
{
4
	var timeline:TimelineLite = new TimelineLite();
5
			
6
	//Rotate the rectangles to their initial position.

7
	new TweenLite(rect1,.3,{rotation2:3});
8
	new TweenLite(rect2,.3,{rotation2:0});
9
	new TweenLite(rect3,.3,{rotation2:-2});
10
11
	//backward the text animation.

12
	timeline.append(new TweenLite(txt,.3,{x:X+65,alpha:.9}));
13
	timeline.append(new TweenLite(txt,.3,{x:X+70}));
14
	new TweenMax(txt,.3,{glowFilter:{color:0xffffff,alpha:0,blurX:0,blurY:0,strength:0,quality:3}});
15
}

Step 25: Mouse Click

This function will open the specified URL when the menu item is clicked.

1
2
private function mouseClick(e:MouseEvent)
3
{
4
	//Open the requested URL.

5
	navigateToURL(new URLRequest(myURL));
6
}

You should add this variable to the variables list.

1
2
private var myURL:String;

And add this instruction to the constructor.

1
2
myURL = URL;

This is what you should get. Roll over the menu to see the animation.

Now let's add a cool bubbles effect.


Step 26: The Bubbles Effect

This function will create a number of bubbles with a random position, size and alpha in two directions. This is the code:

1
2
private function bubbles(position:Number,direction:Number)
3
{
4
	//Create 50 bubbles, you can modify the number to get more or less bubbles.

5
	for (var i=0; i<50; i++)
6
	{
7
		var bubble:DynamicMovie= new DynamicMovie();
8
9
		//Set the registration point for the current bubble.

10
		bubble.setRegistration(X+position,Y);
11
12
		//Give the bubble the same color as the menu item and a random alpha (but greater than 0.2).

13
		bubble.graphics.beginFill(Color,Math.random()+0.2);
14
15
		//draw a circle with a random position and radius.

16
		bubble.graphics.drawCircle(X + position + Math.random() * i,Y + 55  - Math.random() * i,Math.random()*5);
17
18
		//add the bubble at the third index so that it is under the menuButton.

19
		addChildAt(bubble,3);
20
21
		//Tween the Bubble randomly according to the direction.

22
		new TweenLite(bubble,Math.random() + 1,{x2:X - 80 * direction + position - Math.random() * i,y2:Y - Math.random() * i,alpha:0,ease:Circ.easeOut});
23
	}
24
}

Step 27: Call the Bubbles Function

We need to call the bubbles() function when the mouse rolls over the menu item. So add this code to the mouseOver() function:

1
2
//Left bubbles.

3
bubbles(70,1);
4
			
5
//Right bubbles

6
bubbles(270,-1);

This is what we get:


Step 28: Import the Sound Effect

We are going to finish by adding a sound effect to the menu when it is rolled over by the mouse. To do so, download the sound from here (download the mp3 file). Then import it to the library, File > Import > Import to Library. Rename it to "MySound.mp3".

simple Flash menu tutorial

Open its properties and click Advanced; the window will display extra content, select "Export for ActionScript" and name the Class "MySound".

simple Flash menu tutorialsimple Flash menu tutorialsimple Flash menu tutorial

Step 29: Add the Sound Effect to the Menu

To add the sound effect instantiate the sound imported earlier to the library and play it. Place this code in the mouseOver() function.

1
2
var mySound:MySound = new MySound();
3
mySound.play();

We are done with our menu! Here is the full code of the MenuItem.as :

1
2
package 
3
{
4
	import flash.display.Sprite;
5
	import flash.events.Event;
6
	import flash.events.MouseEvent;
7
	import com.greensock.*;
8
	import com.greensock.TweenLite;
9
	import com.greensock.easing.*;
10
	import com.greensock.plugins.*;
11
	import flash.text.TextField;
12
	import flash.media.Sound;
13
	import flash.net.navigateToURL;
14
	import flash.net.URLRequest;
15
16
	public class MenuItem extends Sprite
17
	{
18
		private var rect1:DynamicMovie = new DynamicMovie();//Use the DynamicMovie class instead of Sprite class. 

19
		private var rect2:DynamicMovie = new DynamicMovie();//This allow us to change the registration point.

20
		private var rect3:DynamicMovie = new DynamicMovie();//So we can rotate the rectangels around their centers.

21
		private var menuButton:DynamicMovie = new DynamicMovie();
22
		private var X:Number;
23
		private var Y:Number;
24
		private var Color:uint;
25
		private var txt:Text_mc = new Text_mc();
26
		private var myURL:String;
27
28
		public function MenuItem(posX:Number, posY:Number,color:uint,Title:String,URL:String)
29
		{
30
			//Get the position and color parameters. 

31
			X = posX;
32
			Y = posY;
33
			Color = color;
34
			myURL = URL;
35
36
			// Call the addRect function to add 3 rectangles with the specified parameters.

37
			addRect(rect1,X-12,Y,360,62,Color,0.3,3);
38
			addRect(rect2,X-4,Y,360,62,Color,0.4,0);
39
			addRect(rect3,X,Y,360,62,Color,0.7,-2);
40
41
			//Assign a title to the TextField and place it. 

42
			txt.txtLabel.text = Title;
43
			txt.x = X + 70;
44
			txt.y = Y + 16;
45
			addChild(txt);
46
47
			//Use the addRect function to draw a transparent rectangle over the menu item.

48
			addRect(menuButton,X-10,Y-5,380,80,Color,0,0);
49
			buttonMode = true;
50
51
			menuButton.addEventListener(MouseEvent.MOUSE_OVER,mouseOver);
52
			menuButton.addEventListener(MouseEvent.MOUSE_OUT,mouseOut);
53
			menuButton.addEventListener(MouseEvent.CLICK,mouseClick);
54
		}
55
56
		private function addRect(rect:DynamicMovie,X:Number, Y:Number, width:Number, height:Number,color:uint, alpha:Number, rotation:Number)
57
		{
58
			rect.setRegistration(X+(width/2),Y+(height/2));
59
			rect.graphics.beginFill(color,alpha);
60
			rect.graphics.drawRect(X,Y,width,height);
61
			addChild(rect);
62
			rect.rotation2 = rotation;
63
		}
64
65
		private function mouseOver(e:MouseEvent)
66
		{
67
			var timeline:TimelineLite = new TimelineLite();
68
			var mySound:MySound = new MySound();
69
			mySound.play();
70
71
			//Rotate the rectangels.

72
			new TweenLite(rect1,.3,{rotation2:-4});
73
			new TweenLite(rect2,.3,{rotation2:0});
74
			new TweenLite(rect3,.3,{rotation2:5});
75
76
			//Tween the text.

77
			timeline.append(new TweenLite(txt,.3,{x:X+45,scaleX:1.1,scaleY:1.1}));
78
			timeline.append(new TweenLite(txt,.3,{x:X+70,alpha:1}));
79
80
			//Add a Glow Filter to the text.;

81
			new TweenMax(txt,.3,{glowFilter:{color:0xffffff,alpha:1,blurX:5,blurY:5,strength:1,quality:3}});
82
83
			//Left bubbles.

84
			bubbles(70,1);
85
86
			//Right bubbles

87
			bubbles(270,-1);
88
		}
89
90
		private function mouseOut(e:MouseEvent)
91
		{
92
			var timeline:TimelineLite = new TimelineLite();
93
94
			//Rotate the rectangles to their initial position.

95
			new TweenLite(rect1,.3,{rotation2:3});
96
			new TweenLite(rect2,.3,{rotation2:0});
97
			new TweenLite(rect3,.3,{rotation2:-2});
98
99
			//backward the text animation.

100
			timeline.append(new TweenLite(txt,.3,{x:X+65,alpha:.9}));
101
			timeline.append(new TweenLite(txt,.3,{x:X+70}));
102
			new TweenMax(txt,.3,{glowFilter:{color:0xffffff,alpha:0,blurX:0,blurY:0,strength:0,quality:3}});
103
		}
104
105
		private function mouseClick(e:MouseEvent)
106
		{
107
			//Open the requested URL.

108
			navigateToURL(new URLRequest(myURL));
109
		}
110
111
		private function bubbles(position:Number,direction:Number)
112
		{
113
			//Create 50 bubble, you can modify the number to get more or less bubbles.

114
			for (var i=0; i<50; i++)
115
			{
116
				var bubble:DynamicMovie= new DynamicMovie();
117
118
				//Set the registration point for the bubble.

119
				bubble.setRegistration(X+position,Y);
120
121
				//Give the bubble the same color as the menu item and a random alpha but upper than 0.2.

122
				bubble.graphics.beginFill(Color,Math.random()+0.2);
123
124
				//draw a circle with a random Position and Radius. ;

125
				bubble.graphics.drawCircle(X + position + Math.random() * i,Y + 55  - Math.random() * i,Math.random()*5);
126
127
				//add the bubble at the third index to be under the menuButton.;

128
				addChildAt(bubble,3);
129
130
				//Tween the Bubble randomly according to the direction.

131
				new TweenLite(bubble,Math.random() + 1,{x2:X - 80 * direction + position - Math.random() * i,y2:Y - Math.random() * i,alpha:0,ease:Circ.easeOut});
132
			}
133
		}
134
135
	}
136
}

And here is the final result:


Conclusion

Now you can create your own menu and customize it by adding more menu items, changing the colors, changing the text...

The menu was coded in a separate ActionScript class which allows you to use it easily in other projects.

I want to thank you for reading; I hope you enjoyed it!

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.