Understanding Flash button component

When I am designing a Flash application or a game, I have to say I really hate the process of button creation.

That’s why I am using, from now on, Flash built in button component. It’s fully skinnable, you can add it in a click and supports some interesting features such as some key controls:

Shift+Tab: Moves focus to the previous object.

Spacebar: Presses or releases the button and triggers the click event.

Tab: Moves focus to the next object.

Enter/Return: Moves focus to the next object if a button is set as the FocusManager’s default Button.

To use a button component, just drag the button component from the Components panel to the current document’s Library panel. This adds the component to the library, but doesn’t make it visible in the application.

This is an example that uses multiple instances of the button component:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package {
	import flash.display.Sprite;
	import fl.controls.Button;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	public class buttoncomp extends Sprite {
		var my_text:TextField = new TextField();
		var my_button:Button;
		public function buttoncomp() {
			for (var i:int=0; i<5; i++) {
				my_button=new Button();
				addChild(my_button);
				my_button.label="Button "+i;
				my_button.toggle=true;
				my_button.move(50, i*30+10);
				my_button.addEventListener(MouseEvent.CLICK, clickHandler);
			}
			addChild(my_text);
			my_text.text="Click on a button";
			my_text.width=250;
			my_text.x=50;
			my_text.y=160;
		}
		function clickHandler(event:MouseEvent):void {
			my_text.text="You clicked on: "+event.currentTarget.label;
		}
	}
}

Line 3: Importing the required library to use button component.

Line 8: Declaring the my_button variable, Button type.

Line 10: Cycle to generate five buttons.

Line 11: Creating the button

Line 12: Adding the button to stage

Line 13: Setting the button label

Line 14: Defining the toggle attribute. One advantage of using a Button component over a Button symbol is that the component has the built-in toggle behavior. What is a toggle? Let’s start by talking about a regular button which has two stages; selected or unselected. When you click on a regular button, it will be in the selected stage until you release your mouse. With the release of your mouse, the regular button will automatically go back to the unselected state.

A toggle button also has two states; selected and unselected. The first time that you click on a toggle button, it will be set to the selected state, just like a regular button. However, unlike a regular button that goes back to the unselected state when you release your mouse, a toggle button stays in the selected state. To turn off the toggle, you must click on the toggle button again which will set the toggle back to the unselected state.

Line 15: Positioning the button on the stage

Line 16: Adding a listener to the button

Lines 24-26: The function called by the listener outputs the label of the button I just clicked. Obviously you can perform different actions according to the button you pressed.

That’s what I am going to be in my next game.

Meanwhile play with it:

And download the source code.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 3.67 out of 5)
Loading ... Loading ...
Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 5 comments

  1. dino

    on April 10, 2009 at 5:13 pm

    However, look at what built in components such as buttons does to your file size.

  2. Eric

    on April 10, 2009 at 5:59 pm

    Those keyboard shourcuts have a bug.

    if you hold spacebar on a button and then press shift tab to another button all of the buttons are stuck. :)

  3. Designing the structure of a Flash game - AS3 version - Part 6 : Emanuele Feronato

    on April 11, 2009 at 5:29 pm

    [...] In this 6th part I included MochiBot and I replaced most of the buttons with the button component. [...]

  4. jaco carstens

    on October 11, 2009 at 4:49 pm

    I’d like to know how to duplicate a button component, so I can change the skins of each button component… eg. have 5 dynamic buttons on stage, each one looking differently in AS3?? Anybody?

  5. Official Facebook Actionscript API released – AS3 version : Emanuele Feronato

    on October 28, 2009 at 1:39 pm

    [...] post contains a script made combining Official Facebook Actionscript API released! with Understanding Flash button component. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 [...]