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:

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.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

215 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stairs
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers