1. Code
  2. JavaScript

Create an iPhone Inspired Switch CheckBox Using Flash and ActionScript 3.0

Scroll to top
5 min read

A CheckBox is a graphical user interface element that permits the user to make one or multiple selections from a number of options.

In this tutorial, we'll create a Switch checkbox inspired by the iPhone Graphical User Interface. Read on to find out how!

Step 1: Brief Overview

Using the Flash drawing tools we'll create a vector Switch that will be controled by classes. One class will take care of all the Switch behavior and another class will simply check the value of the Switch. Let's go!

Step 2: Starting

Open Flash and create a new Flash File (ActionScript 3).

Set the stage size to 600x300 and set the color to #EFEFF0.

””””””

We'll now create the Switch graphics.

Step 3: Border

Select the Primitive Rectangle Tool (R) and create a 280x80 px rectangle, filling it with this linear gradient: #505052, #ACADB1.

Use the Gradient Transform Tool to rotate the gradient horizontally and change the corner radius (Properties Panel) to 10.

Step 4: OFF Background

We'll draw two backgrounds for the Switch, the OFF background and the ON background.

Duplicate the previous shape and change its size to 276x76 px. Change the linear grandient to #9A9A9A, #F4F3F6 and move the last color selector (Color Panel) to halfway along the bar.

Select the Text Tool (T) and create a Static TextField. Write "OFF" and place it at the right side of the background.

I used Helvetica Neue Bold, 48 pt, #8C8C8C.

Step 5: Draggable Area

Now we'll add a button that can be dragged to modify the Switch value.

Use the Rectangle Tool to create a 120x80 px rectangle and fill it with #A19FA0, set the corner radius to 10.

Duplicate the shape and resize it to 116x76 px, fill it with #FCFCFE.

To give the final touch to the button, repeat the process and fill the shape with a #D7D7D7, #FCFCFE linear gradient. Use the Gradient Transform Tool to rotate the fill.

Step 6: ON Background

Duplicate the border and the OFF background, delete the text and change the border gradient to #0D4372, #6193D2.

Next, change the background gradient to #0C68B5, #479FF9, #6DB0F6.

Place the button border shape in the right side.

Break Apart (Cmd+B) the shapes to cut them.

Use the same Text Format to add the "ON" text to the background.

Step 7: Setting the MovieClips

Convert the Draggable Button to MovieClip and name it "area". As you can imagine this will be the area that will be dragged to change the Switch value.

Make sure the Registration point is positioned like the one in the images.

Select all shapes including the MovieClip and convert them again, name the result "slider".

Use any of the border shapes to create another MovieClip, this will be the Mask that will hide part of the graphics. Name it "msk".

Convert everything to MovieClip once again and double-click it.

Create a new Layer then cut and paste the mask clip on it. Right-click the mask layer and select the "Mask" option.

This will finish all the graphics. Now your Switch should look like this (note the Registration point):

Step 8: Linkage

Open the Library and right-click your Switch symbol. Select Properties, mark the "Export for ActionScript" box and write "Switch" as the class name.

Step 9: Switch.as

Create a new ActionScript document and save it as "Switch.as".

Step 10: Necessary Classes

Import the required classes. If you need specific help for any of these, please refer to the Flash Help (F1).

1
2
package 
3
{
4
	import fl.transitions.Tween;
5
	import fl.transitions.easing.Strong;
6
	import flash.display.Sprite;
7
	import flash.events.MouseEvent;
8
	import flash.geom.Rectangle;

Step 11: Variables

These are the variables we'll use, explained in the code commentary.

1
2
public class Switch extends Sprite
3
{
4
	private var tween:Tween; //A Tween object for animation

5
	public var stat:Boolean = false; // This is a Public variable, it's used to know the Switch value outside this class

Step 12: Constructor Function

The Constructor function. This function adds the listeners.

1
2
public function Switch():void
3
{
4
	slider.area.addEventListener(MouseEvent.MOUSE_DOWN, switchDrag);
5
	slider.area.addEventListener(MouseEvent.MOUSE_UP, checkPosition);
6
}

Step 13: Drag function

This function handles the button dragging, based on its position.

1
2
private function switchDrag(e:MouseEvent):void
3
{
4
	if (! stat) //If Switch is OFF, we can drag to the right

5
	{
6
		e.target.parent.startDrag(true, new Rectangle(0, 0, e.target.parent.parent.msk.width/1.75, 0));
7
	}
8
	else
9
	{
10
		e.target.parent.startDrag(true, new Rectangle(e.target.parent.parent.msk.width/1.75, 0, -e.target.parent.parent.msk.width/1.75, 0));
11
	}
12
}

Step 14: Check Function

This code checks the position of the draggable button. Depending on its value it returns to the original position or stays in the new one.

1
2
private function checkPosition(e:MouseEvent):void
3
{
4
	e.target.parent.stopDrag();
5
6
	if (e.target.parent.x >= 140)
7
	{
8
		e.target.parent.x = 160;
9
		stat = true;
10
	}
11
	else if (!stat && e.target.parent.x < 140)
12
	{
13
		tween = new Tween(e.target.parent,"x",Strong.easeOut,e.target.parent.x,0,1,true);
14
		stat = false;
15
	}
16
17
	// OFF to ON

18
19
	if (e.target.parent.x <= 20)
20
	{
21
		e.target.parent.x = 0;
22
		stat = false;
23
	}
24
	else if (stat && e.target.parent.x > 20)
25
	{
26
		tween = new Tween(e.target.parent,"x",Strong.easeOut,e.target.parent.x,160,1,true);
27
		stat = true;
28
	}
29
}

Step 15: Main Class

This is an example of how to use your new Switch.

Create a new ActionScript document and save it as "Main.as".

1
2
package
3
{
4
	import Switch; //Import the class

5
	import flash.display.Sprite;
6
	import flash.events.MouseEvent;
7
	
8
	public class Main extends Sprite
9
	{
10
		public function Main():void
11
		{
12
			iSwitch.addEventListener(MouseEvent.MOUSE_UP, checkState);//iSwitch is an instance in the stage of the Switch class

13
		}
14
15
		private function checkState(e:MouseEvent):void
16
		{
17
			if(iSwitch.stat)
18
			{
19
				trace("Switch is ON!");
20
			}
21
			else
22
			{
23
				trace("Switch is OFF!");
24
			}
25
		}
26
	}
27
}

Step 16: Document Class

Go back to the .Fla file and in the Properties Panel add "Main" in the Class field to make this the Document Class.

Conclusion

You have created a fully customizable Switch to use in your applications! Remember that you can create your own skins and add plenty more functionality to the ON and OFF states.

Thanks for reading!

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.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.