# FlashPunk UI Components: Advanced Graphics, Extra Skins, and Polish

##### Tutorial Details
• Difficulty: Intermediate
• Platform: Flash
• Language: AS3
• Software used: Flash Develop + FlashPunk
• Estimated Completion Time: 45 minutes
This entry is part 2 of 2 in the series FlashPunk UI Components

In this tutorial we’ll finish what we started in the first part of this UI with FlashPunk mini-series. We’ll learn how to customize the graphics of our components, and make a few adjustments to make our UI perfect.

## Final Result Preview

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

Make sure you click everything to see how it responds!

## Step 1.2: Drawing Stuff

The next step we need to do to be able to extensively customize the graphic side of our UI is learn how to draw the graphic stuff directly with code. At the momment we needed an image for each state of our UI (button normal, button hover, checkbox normal unselected, checkbox normal selected, etc.), so we needed a large ammount of Images. Now, let’s say you want a different color for each button, or even a different shape for each one, and selected randomly… we can’t afford to have millions of images: too work to make them and they consume too much memory.

But before starting to draw the components itself, we must first learn how to draw stuff using code in FlashPunk.

## Step 1.3: Preparing a Drawing Test World

First of all, we need a new World to test the Drawing stuff. Create a new World class, called DrawWorld, and tell Main to go there when starting the game.

DrawWorld.as:

package
{
import net.flashpunk.World;

public class DrawWorld extends World
{
public function DrawWorld()
{

}
}
}


## Step 1.4: Using the FlashPunk Drawing Utils

The FlashPunk Engine has a few utils which lets us Draw things to the screen or to a BitmapData.

These functions are: line, linePlus, rect, rectPlus, circle, circlePlus and curve (the other ones are used to draw other kinds of stuff). The ‘plus’ versions come with more options and give better results (using antialiasing) but are more CPU intensive.

Using them is easy; override the render method of your World or an Entity and call those functions, like this overriden render function for our DrawWorld, which draws some shapes:

		override public function render():void
{
super.render();

Draw.line(10, 10, 100, 100, 0x00FF00);
Draw.rectPlus(120, 120, 200, 200, 0x9999FF, 1, true, 2, 10);
Draw.circle(100, 100, 50, 0xFF0000);
Draw.curve(100, 100, 140, 90, 122, 122, 0x00FFFF, 1, 3);
}


## Step 1.5: That’s Slow!

The problem with that method is, it’s really slow. We’re drawing the same stuff over and over again, for no reason, to the screen. This would be OK if things were changing EVERY frame, but if they are not, it’s a complete waste of resources.

What we’re going to do instead is, create a bitmapData to store our drawings, then draw them there once or just every time they change. Then we just need to render that bitmapData to the screen using copyPixels, which is a much faster way of doing it… or we’ll add a Stamp, which is a FlashPunk graphic that does exactly that: draw a bitmapData to the screen.

To make Draw do the drawing to a custom bitmapData instead of using the screen, we can use its setTarget function. Draw.resetTarget will make it draw future commands to the screen again.

Here’s our DrawWorld code now. If you test it, the result should be exactly the same as before.

package
{
import flash.display.BitmapData;
import net.flashpunk.graphics.Stamp;
import net.flashpunk.utils.Draw;
import net.flashpunk.World;

public class DrawWorld extends World
{
override public function begin():void
{
super.begin();

var picture:BitmapData = new BitmapData(300, 300, true, 0);
Draw.setTarget(picture);

Draw.line(10, 10, 100, 100, 0x00FF00);
Draw.rectPlus(120, 120, 200, 200, 0x9999FF, 0.5, true, 2, 10);
Draw.circle(100, 100, 50, 0xFF0000);
Draw.curve(100, 100, 140, 90, 122, 122, 0x00FFFF, 1, 3);

}
}
}


Okay. We know how to draw some circles, lines, squares, and so on to the screen and to a BitmapData now. Thing is, I want to make a good-looking button, not a lame button made of a simple square and a circle! That’s why we need advanced drawing.

Advanced drawing is just some concept I made up to refer to using the AS3 Drawing API directly (you know, when you draw using the graphics of a Sprite, Shape, etc.). The process is, basically: First, create a Sprite so we can get a Graphics class. Then, we draw what we need to the Sprite graphics. Finally, we render the Sprite to a BitmapData. Done!

Using this process, we can draw advanced shapes and use advanced fills, like making some awesome-looking gradient buttons. Yay!

I’m not going to explain each individual method of the Draw API, as explaining it isn’t the scope of this tutorial, but there are plenty of resources on the net if you don’t know how it works.

The Drawing API is used on the Graphics class. But you can’t create a Graphics class on-the-fly, and you wouldn’t be able to “draw” it anywhere. Instead, we can create a Sprite and use its graphics property for drawing. Then, to use that Sprite as a FlashPunk graphic, we simply render it to a BitmapData.

Here’s some example code, in which I draw a gradiented rectangle:

package
{
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.Sprite;
import flash.geom.Matrix;
import net.flashpunk.FP;
import net.flashpunk.graphics.Stamp;
import net.flashpunk.World;

public class DrawWorld extends World
{
override public function begin():void
{
super.begin();

var sprite:Sprite = new Sprite;
var g:Graphics = sprite.graphics;

g.drawRoundRect(0, 0, 200, 50, 20);
g.endFill();

var bd:BitmapData = new BitmapData(200, 50, true, 0);
bd.draw(sprite);

}
}
}
• Lines 17-18: We create a Sprite to use its graphics property for drawing, and we store its graphics to a variable named g for less typing.
• Lines 20-21: We create a gradient matrix, which is required to have a gradient fill. We provide it the size of our rectangle, and a rotation in radians (we convert 270 degrees to radians by multiplying by FP.RAD). As the rectangle will be drawn at (0,0), we set tx and ty properties to 0.
• Lines 22-24: We draw the rounded rect with the gradient fill to the sprite graphics.
• Lines 26-27: We create a BitmapData the size of the graphic, and render the sprite to it.
• Line 29: We create a Stamp, providing our BitmapData which has the sprite rendered, and add it to the World at (100, 50).

Result:

## Step 1.7: Filters!

We’re now ready to draw gorgeous buttons… but we can step it up a bit and make them look even better! To do that, on top of the graphics itself, we can add filters to them. With Flash, we can add drop shadow filters, glow filters, bevel filters, and so on, easily.

To add filters, we simply need to populate the sprite’s filter array with our filters – or if we want to use BitmapFilters, we use bd.applyFilter() after we’ve drawn the sprite.

Let’s add an inner glow filter and a drop shadow filter to our rectangle, and also change the color of the screen, so we can preview it better. Code here:

		override public function begin():void
{
FP.screen.color = 0xD9F2FF;

super.begin();

var sprite:Sprite = new Sprite;
var g:Graphics = sprite.graphics;

g.drawRoundRect(0, 0, 200, 50, 20);
g.endFill();

sprite.filters = [new GlowFilter(0xFFFFFF, 1, 10, 10, 1, 3, true), new DropShadowFilter(8, 45, 0, 0.8, 10, 10, 1, 3)];

var bd:BitmapData = new BitmapData(200, 50, true, 0);
bd.draw(sprite);

}


If you preview the current code, you’ll see the filters look somewhat… cropped.

Let’s fix that!

## Step 1.8: Quick Fix for the Filter Crop

First of all… why does it look cropped? Basically, the problem is our BitmapData is the size of the button, but the shadow is bigger than the button. So, when rendering the sprite to the bitmapdata, as the shadow doesn’t fit the BitmapData, it gets cropped.

The easiest way to solve that is simply to enlarge the BitmapData. We’ll also have to give the sprite some x and y offset, as the shadow is slightly on top and left of the button as well.

			//[...]

sprite.filters = [new GlowFilter(0xFFFFFF, 1, 10, 10, 1, 3, true), new DropShadowFilter(8, 45, 0, 0.8, 10, 10, 1, 3)];

var bd:BitmapData = new BitmapData(250, 100, true, 0);
var m:Matrix = new Matrix;
m.tx = 20;
m.ty = 20;
bd.draw(sprite, m);

var stamp:Stamp = new Stamp(bd);
stamp.x = -m.tx;
stamp.y = -m.ty;


We also offset the stamp’s x and y to the negative offset of the bitmap drawing. This way, the button coordinates (100, 50) originate from the rounded rectangle and not from the shadow, and it’s in the same place as before.

## Step 1.9: Endless Possibilities!

As you can see, combining the AS3 Drawing API and its filters can lead to impressive results. The possibilities are endless, if you know well how it works, so it might be worth reading a few things about the Drawing API and filters.

For example, here you have a pack of AS3 filter setups, with amazing results: Flash Effects Pack. And a cool static distortion filter: Static Distortion Effect using the DisplacementMapFilter.

There are thousands more of tutorials and effects using flash filters and the drawing API spread all over the web, which you could apply to your UI set. Even better: if you have the knowledge, you can create those amazing effects by yourself.

## Step 2.1: Refactoring the UI Code

Let’s implement all those drawing stuff to our UI. First of all, as having to write all the code needed to go from a Sprite to a BitmapData for each element we need to draw is a bit silly, we’ll just create a helper function that will do that for us every time we need it. This way we don’t repeat code over and over again.

We’ll place this helper function in a base class all the UI elements will extend. Let’s call it Component, place it in the ui folder, and make it extend Entity:

package ui
{
import net.flashpunk.Entity;

public class Component extends Entity
{
public function Component(x:Number = 0, y:Number = 0)
{
super(x, y);
}
}
}

Now we need to change our ui classes to extend this instead of Entity. We’ll only need to change Button and TextInput, as Checkbox extends Button so it will extend Component indirectly, and RadioButton extends Checkbox so the same happens.

## Step 2.2: Helper Function

Taking as a base the code we made for drawing our cool gradient-y button, we’ll build this helper function. Here’s how stuff will work:

Component will have a protected sprite and g variables. This way, you can add filters to Sprite and draw stuff on graphics. They will be kept alive forever, so it’s your job to clean them if you’re going to draw multiple stuff on the same component, and destroy them completely when they aren’t needed anymore.

Then, when calling this function, it will generate a BitmapData of the specified size and draw the sprite to it. Then it will return a Stamp containing that BitmapData, ready to use!

Even though there’s already a graphic and sprite properties which can be cleant and used multiple times, if you really need to use a different sprite, the function will be able to get it through a parameter. By default, the parameters will point to the sprite member of the class.

package ui
{
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.Graphics;
import flash.display.Sprite;
import flash.geom.Matrix;
import net.flashpunk.Entity;
import net.flashpunk.graphics.Stamp;

public class Component extends Entity
{
protected var sprite:Sprite;
protected var g:Graphics;

public function Component(x:Number = 0, y:Number = 0)
{
super(x, y);

sprite = new Sprite;
g = sprite.graphics;

_m = new Matrix();
}

private var _m:Matrix;

protected function drawStamp(width:Number, height:Number, offsetX:Number = 0, offsetY:Number = 0, sprite:DisplayObject = null):Stamp
{
if (sprite == null) sprite = this.sprite;

var bd:BitmapData = new BitmapData(width, height, true, 0);
_m.tx = offsetX;
_m.ty = offsetY;
bd.draw(sprite, _m);

return new Stamp(bd, -offsetX, -offsetY);
}
}
}

## Step 2.3: Removing Graphics From UI

We will take a different approach for skinning now. The base components (Button, Checkbox, etc.) will only hold the behaviour itself, without the graphics. Then we can extend them (to MenuButton, GameButton, MuteButton, etc.) to include the specific graphic behaviour.

So first of all, we must remove the graphic rendering of our components. We’ll also make the player input the size of the components.

## Step 2.3a: Removing Graphics From Button

For the button, first of all, we need to remove the graphic creation, and the variables for it. We’ll also replace the label:Text property with a string property called text. And we’ll require a width and height for setting the hitbox.

	public class Button extends Component
{
protected const NORMAL:int = 0;
protected const HOVER:int = 1;
protected const DOWN:int = 2;

public var clicked:Boolean = false;

protected var text:String;
public var callback:Function;
public var params:Object;

public function Button(x:Number=0, y:Number=0, text:String = "", width:Number = 150, height:Number = 50, callback:Function = null, params:Object = null)
{
super(x, y);

this.callback = callback;
this.params = params;
this.text = text;

setHitbox(width, height);
}

//[...]
}


Then, we remove the renderGraphic function and place it in Component instead. We empty the changeState function, because it will be handled on the skinning classes. The render override will also be removed, because we don’t have a label to render. So, after the update function, which we don’t need to change, all we have is this:

		protected function changeState(state:int = 0):void
{
}

protected function click():void
{
if (callback != null)
{
if (params != null) callback(params);
else callback();
}
}


We also could add a lastState variable, and set it on the changeState function, in case our skins need it. Remember, as lastState will be set in the changeState function, when overriding it we will place the new behaviour first and do the super call at the end.

		protected var lastState:int = 0;

protected function changeState(state:int = 0):void
{
lastState = state;
}


## Step 2.3b: Removing Graphics From RadioButton

For the checkbox, we’ll need to remove the changeState override and all the graphic creation in the constructor. For the radio button, we’ll just need to remove the graphic creation in the constructor. We’ll also add the width and height parameters on both classes.

New Checkbox:

package ui
{
public class Checkbox extends Button
{
public var checked:Boolean = false;

public function Checkbox(x:Number=0, y:Number=0, text:String = "", width:Number = 150, height:Number = 150, callback:Function = null, params:Object = null, checked:Boolean = false)
{
super(x, y, text, width, height, callback, params);

this.checked = checked;
}

override protected function click():void
{
checked = !checked;

if (callback != null)
{
if (params != null) callback(checked, params);
else callback(checked);
}
}
}
}


package ui
{
{

public function RadioButton(x:Number=0, y:Number=0, group:RadioButtonGroup = null, text:String = "", width:Number = 150, height:Number = 50, params:Object = null, checked:Boolean = false)
{
super(x, y, text, width, height, null, params, checked);

}

override protected function click():void
{
group.click(this, params);
}

override public function removed():void
{
super.removed();

group.remove(this);
}
}
}


## Step 2.3c: Removing Graphics From TextInput

For the text input, all we need to remove is the textGraphic property, and its creation. We’ll also add an onFocus function so we can handle graphic changes for focus on the skin.

package ui
{
import flash.events.KeyboardEvent;
import net.flashpunk.FP;
import net.flashpunk.utils.Input;
import net.flashpunk.utils.Key;

public class TextInput extends Component
{
protected var _text:String = "";

protected var multiline:Boolean = false;

public static var focus:TextInput;
private var _focused:Boolean = false;

public function TextInput(x:Number=0, y:Number=0, text:String = "", multiline:Boolean = false, width:Number = 150, height:Number = 30)
{
super(x, y);

this.multiline = multiline;

this.text = text;

type = "uiTextInput";

setHitbox(width, height);
}

{

}

protected function onKeyDown(e:KeyboardEvent):void
{
if (world != FP.world) return;
if (TextInput.focus != this) return;

if (e.keyCode == Key.BACKSPACE) text = _text.substr(0, _text.length - 1);
else if (e.keyCode == Key.ENTER && multiline) text += "\n";
}

override public function removed():void
{
super.removed();

FP.stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
}

protected function onFocus(focused:Boolean):void
{

}

override public function update():void
{
super.update();

if (TextInput.focus == this)
{
if (!_focused)
{
onFocus(true);
_focused = true;
}
}
else if (_focused)
{
onFocus(false);
_focused = false;
}

if (Input.mousePressed)
{
if (collidePoint(x, y, world.mouseX, world.mouseY))
{
TextInput.focus = this;
Input.keyString = "";
}
else if (!world.collidePoint("uiTextInput", world.mouseX, world.mouseY)) TextInput.focus = null;
}

if (TextInput.focus == this && Input.keyString != "")
{
text += Input.keyString;
Input.keyString = "";
}
}

public function get text():String
{
return _text;
}

public function set text(value:String):void
{
_text = value;
}
}
}


## Step 2.4: Preparing to Test

Everything’s prepared to begin building our skins! But first, we need to point to our TestWorld again. To do that, change the line in the Main class which points to the DrawWorld, and make it point to TestWorld.

If you test it now, you’ll see a blank world. That’s to be expected; we removed all the graphics from our components. Don’t worry, though, we’ll begin the skinning now!

## Step 3.1: Skinning the Button

Now we’re going to go through, step by step, the whole process of skinning a component, taking as an example the button. We will see the most common techniques you will need when skinning your own components: drawing the graphics, adding some fancy animations, some cool sound effects, particle eye-candy, and even some pixel-perfect collision techniques and changing the mouse icon to a hand when hovering over the component.

This should basically prepare you to be able to skin any component you want with your own style and without any problem. We will then apply the knowledge we gained with the button to skin the checkbox and radio button, we’ll learn how to skin a more complex and different button like the text input, and then we’ll build an extra component and skin it.

## Step 3.2: Normal Graphic

First of all we’ll make a button show its normal graphic. We’ll use the same cool graphic we used in the Draw World. So, basically, we copy what we used there and then call the drawStamp function to make the Sprite be transformed into a Stamp, and we’ll set it to our button’s graphic.

Before that, though, we need to actually have a button! Create a blue folder inside the src folder. This is where we’ll place all our skinned components. We’ll make them blue, as you can guess by the name. This is just for us to keep things organised. In your game, you’d probably want to put different folders with different skins in your ui folder, and call them something like menu, game, etc.

Then, make a BlueButton class which extends Button in the blue folder:

package blue
{
import ui.Button;

public class BlueButton extends Button
{
public function BlueButton(x:Number=0, y:Number=0, text:String="", width:Number=150, height:Number=50, callback:Function=null, params:Object=null)
{
super(x, y, text, width, height, callback, params);

}
}
}


Now, we’ll place the drawing code in the constructor:

		public function BlueButton(x:Number=0, y:Number=0, text:String="", width:Number=150, height:Number=50, callback:Function=null, params:Object=null)
{
super(x, y, text, width, height, callback, params);

g.drawRoundRect(0, 0, 200, 50, 20);
g.endFill();

sprite.filters = [new GlowFilter(0xFFFFFF, 1, 10, 10, 1, 3, true), new DropShadowFilter(8, 45, 0, 0.8, 10, 10, 1, 3)];

graphic = drawStamp(220, 70, 5, 5);
}


To test it, we need to create the button in TestWorld. So, comment or remove all the components created there, and place a new BlueButton instead. Also, change the background color to a lighter one. Here’s my TestWorld begin function:

		override public function begin():void
{
super.begin();

FP.screen.color = 0xD9F2FF;

add(new BlueButton(20, 100, "Fat button", 500, 200));
}


If you test the project, you’ll see the buttons are being drawn! However, they all are the same size, even though we specified the fat button to be very big. Let’s give the button some cake.

## Step 3.3: Button Graphic Size

First of all, I’d like to move the function which draws the button’s graphic into a helper function of BlueButton. Keeps stuff cleaner.

	public class BlueButton extends Button
{
public function BlueButton(x:Number=0, y:Number=0, text:String="", width:Number=150, height:Number=50, callback:Function=null, params:Object=null)
{
super(x, y, text, width, height, callback, params);

graphic = drawButton(width, height);
}

protected function drawButton(width:Number, height:Number):Stamp
{
g.drawRoundRect(0, 0, 200, 50, 20);
g.endFill();

sprite.filters = [new GlowFilter(0xFFFFFF, 1, 10, 10, 1, 3, true), new DropShadowFilter(8, 45, 0, 0.8, 10, 10, 1, 3)];

return drawStamp(220, 70, 5, 5);
}
}


All we did is move the button draw stuff from the constructor to a helper function. This helper function returns the Stamp, and asks for a width and a height. We set our graphic to the Stamp returned. Now we need to make it actually use the width and height provided.

All the changes we need to do are in the matrix gradient box, the round rect drawing and in the stamp creation. The stamp creation needs to have a bigger size, because of the filters. Here’s the final code:

		protected function drawButton(width:Number, height:Number):Stamp
{
g.drawRoundRect(0, 0, width, height, 20);
g.endFill();

sprite.filters = [new GlowFilter(0xFFFFFF, 1, 10, 10, 1, 3, true), new DropShadowFilter(8, 45, 0, 0.8, 10, 10, 1, 3)];

return drawStamp(width + 20, height + 20, 5, 5);
}


And here’s the result! They don’t do anything when you click them, but they’re actual buttons!

## Step 3.4: Glare and Border

Let’s make our button a bit better by adding a border and a glare to it.

Adding a border is very easy. We could do two things: apply a line style to the button graphics before drawing the round rect, or use a filter. The former would make our button look weird, though, as the inner glow will also cover the border, so let’s go for the filter approach.

Even though there’s no specific BorderFilter in Flash, the effect can easily be achieved with a glow filter that has high strength and low blur. We’ll add this filter between the glow filter and the shadow filter, like this:

		protected function drawButton(width:Number, height:Number):Stamp
{
g.drawRoundRect(0, 0, width, height, 20);
g.endFill();

sprite.filters = [new GlowFilter(0xFFFFFF, 1, 10, 10, 1, 3, true), new GlowFilter(0x00456A, 0.6, 2, 2, 6, 2), new DropShadowFilter(8, 45, 0, 0.8, 10, 10, 1, 3)];

return drawStamp(width + 20, height + 20, 5, 5);
}


The glare will be a little more complicated, but will introduce us to a new concept you will probably need for your own components: multiple graphics. As of now, the skin only consists of a single graphic. But what if we need to add more to it, like a glare?

We can take two approaches: using a Graphiclist, or using the renderGraphic function our components have. We’ll use the renderGraphic function because our button graphic will change for the hover function, while the glare will remain the same. This way we don’t have to bother updating the graphiclist.

To draw the glare, all we got to do is draw a semi-transparent big white ellipse, covering the button. Then, to avoid the glare covering everything in the world, we can mask the glare sprite using our button’s graphic. Here’s the code:

		protected var glare:Stamp;

public function BlueButton(x:Number=0, y:Number=0, text:String="", width:Number=150, height:Number=50, callback:Function=null, params:Object=null)
{
super(x, y, text, width, height, callback, params);

graphic = drawButton(width, height);

drawGlare();
}

protected function drawGlare():void
{

sprite.filters = [];
g.clear();
g.beginFill(0xFFFFFF, 0.25);
g.drawEllipse( -width * 0.25, -height * 0.6, width * 1.5, height);

glare = drawStamp(width, height * 0.4);
}


And the rendering:

		override public function render():void
{
super.render();

renderGraphic(glare);
}


Here’s the end result. Beautiful, isn’t it?

## Step 3.5: Hover Graphic

To make the hover graphic, we’ll use the same drawButton function but we’ll make the button red-orangish. To do that, we’ll need to ask for some color parameters on that function.

		protected function drawButton(width:Number, height:Number, topColor:uint, bottomColor:uint, borderColor:uint):Stamp
{
g.drawRoundRect(0, 0, width, height, 20);
g.endFill();

sprite.filters = [new GlowFilter(0xFFFFFF, 1, 10, 10, 1, 3, true), new GlowFilter(borderColor, 0.6, 2, 2, 6, 2), new DropShadowFilter(8, 45, 0, 0.8, 10, 10, 1, 3)];

return drawStamp(width + 20, height + 20, 5, 5);
}


Now, our normal graphic needs the same colors!

		public function BlueButton(x:Number=0, y:Number=0, text:String="", width:Number=150, height:Number=50, callback:Function=null, params:Object=null)
{
super(x, y, text, width, height, callback, params);

graphic = drawButton(width, height, 0x6ACFFF, 0x005F8C, 0x00456A);

drawGlare();
}


To make the normal / hover switch, we’ll store each graphic and make the correspondent switch in the changeState function. So first, let’s make a variable for each graphic and populate it:

		protected var glare:Stamp;

protected var normal:Stamp;
protected var hover:Stamp;

public function BlueButton(x:Number=0, y:Number=0, text:String="", width:Number=150, height:Number=50, callback:Function=null, params:Object=null)
{
super(x, y, text, width, height, callback, params);

normal = drawButton(width, height, 0x6ACFFF, 0x005F8C, 0x00456A);
hover = drawButton(width, height, 0xFF8C66, 0xDD0500, 0x550000);
graphic = normal;

drawGlare();
}


Now we switch to the correspondent graphic on the changeState function:

		override protected function changeState(state:int = 0):void
{
if (state == lastState) return;

switch(state)
{
case NORMAL:
graphic = normal;
break;
case HOVER:
graphic = hover;
break;
}

super.changeState(state);
}


And here’s the result! Finally, interactive buttons! (…which do nothing)

## Step 3.6: Hover Animation Preparation

Let’s say we want to add a tween for the hover graphic, instead of changing the graphic immediately on hover.

To be able to do that, we need to make a change first. As creating a new Stamp and a new BitmapData each frame would be a waste of resources, we’ll adapt the drawButton function to draw to a given BitmapData instead. Then, we will simply use that BitmapData for a permanent Stamp in our button.

		protected function drawButton(bitmap:BitmapData, width:Number, height:Number, topColor:uint, bottomColor:uint, borderColor:uint):void
{
g.clear();
g.drawRoundRect(0, 0, width, height, 20);
g.endFill();

sprite.filters = [new GlowFilter(0xFFFFFF, 1, 10, 10, 1, 3, true), new GlowFilter(borderColor, 0.6, 2, 2, 6, 2), new DropShadowFilter(8, 45, 0, 0.8, 10, 10, 1, 3)];

_m.tx = 5;
_m.ty = 5;
bitmap.draw(sprite, _m);
}
private var _m:Matrix = new Matrix;


Now we create the Stamp which will hold it. First, we’ll remove the current stamps we’ve got for the separate states, and use this one instead.

		protected var buttonBmp:BitmapData;
protected var buttonGraphic:Stamp;

public function BlueButton(x:Number=0, y:Number=0, text:String="", width:Number=150, height:Number=50, callback:Function=null, params:Object=null)
{
super(x, y, text, width, height, callback, params);

buttonBmp = new BitmapData(width + 20, height + 20, true, 0);
drawButton(buttonBmp, width, height, 0x6ACFFF, 0x005F8C, 0x00456A);

buttonGraphic = new Stamp(buttonBmp, -5, -5);
graphic = buttonGraphic;

drawGlare();
}


Time to tween!

## Step 3.7: Tweening Button Colors

To tween the colors for the hover and normal states of our button, we will use the TweenLite library. How to use it is outside the scope of this tutorial, but there are plenty of resources on the net and here on Activetuts+.

(We could also use any other tweening library, or even FlashPunk’s own tweening. But TweenLite is more extensive than FlashPunk Tweening, is the library which I know better and it’s one of the best tweening libraries for AS3.)

So, go download TweenLite and add it to your code. As I use FlashDevelop, I got the SWC, placed it in the lib folder and right-clicked it to add it to my project library.

First of all, we’ll create all the variables we need to tween on our button. In our case, it’s just three colours, but it can be whatever you want! They need to be public, so TweenLite can access them.

		public var topColor:uint = 0x6ACFFF;
public var bottomColor:uint = 0x005F8C;
public var borderColor:uint = 0x00456A;


We also need to activate a plugin in TweenLite so we can tween colors. Place this in Main:

TweenPlugin.activate([HexColorsPlugin]);


All we need to do now is tween those properties in the changeState function:

		override protected function changeState(state:int = 0):void
{
if (state == lastState) return;

switch(state)
{
case NORMAL:
TweenLite.to(this, 0.25, { hexColors: { topColor: 0x6ACFFF, bottomColor: 0x005F8C, borderColor: 0x00456A }, onUpdate: updateGraphic } );
break;
case HOVER:
TweenLite.to(this, 0.25, { hexColors: { topColor: 0xFF8C66, bottomColor: 0xDD0500, borderColor: 0x550000 }, onUpdate: updateGraphic } );
break;
}

super.changeState(state);
}


We could do a lot of fancy stuff, like easing or using other TweenLite features, but we’ll keep it simple for this example. Remember, you can use whatever you want for your skins.

As you can see, we also added an onUpdate parameter. That’s so the graphic can be updated according to the tween values, and we actually see it. Here’s the function:

		protected function updateGraphic():void
{
buttonBmp.fillRect(buttonBmp.rect, 0);
drawButton(buttonBmp, width, height, topColor, bottomColor, borderColor);
}


And here’s the result!

## Step 3.8: Down State

For the down state, we’ll simply quickly tween the button to black. You already know how to do this, just add another case with the tween parameters.

				case DOWN:
TweenLite.to(this, 0.1, { hexColors: { topColor: 0x5B5B5B, bottomColor: 0x2D2D2D, borderColor: 0x151515 }, onUpdate: updateGraphic } );
break;


The only problem with this is, when going from down to another state, the transition is, in my opinion, too slow. Let’s make it faster. As we want two different durations, the regular one and the “from down” one, we’ll make an extra variable in that function. This variable will check the state we come from, and set the duration depending on that. Then, the NORMAL and HOVER transitions will take into account this variable.

Here’s the function:

		override protected function changeState(state:int = 0):void
{
if (state == lastState) return;

var duration:Number = 0.25;
if (lastState == DOWN) duration = 0.15;

switch(state)
{
case NORMAL:
TweenLite.to(this, duration, { hexColors: { topColor: 0x6ACFFF, bottomColor: 0x005F8C, borderColor: 0x00456A }, onUpdate: updateGraphic } );
break;
case HOVER:
TweenLite.to(this, duration, { hexColors: { topColor: 0xFF8C66, bottomColor: 0xDD0500, borderColor: 0x550000 }, onUpdate: updateGraphic } );
break;
case DOWN:
TweenLite.to(this, 0.1, { hexColors: { topColor: 0x5B5B5B, bottomColor: 0x2D2D2D, borderColor: 0x151515 }, onUpdate: updateGraphic } );
break;
}

super.changeState(state);
}


And here’s the final result. Click our buttons!

## Step 3.9: The Label

Our button has all the graphics, animations and behaviours. But it needs a very important thing, so the user knows the purpose of each button: a label.

We are already sending some text parameters to our button, so the only thing we need to do is create a label graphic with that text property. We could use FlashPunk’s Text class with the renderGraphic function on the render override, but I would like to add some filters to our label, so we’ll create the text field directly, and use a Stamp.

First of all, we’ll prepare the Stamp, its creation and its render, and we’ll use a function called renderLabel which will render that label into the stamp. We’ll code the function later.

We create the label property which will hold our label Stamp:

	public class BlueButton extends Button
{
protected var glare:Stamp;
protected var label:Stamp;
//[...]


The end of the constructor, where we call the drawLabel function after the drawGlare one:

			//[...]
drawGlare();
drawLabel(text);
}


The drawLabel function, which is empty at the moment:

		protected function drawLabel(text:String):void
{
}


And our render function, rendering the label right after the glare, so it’s the top-most element of the button:

		override public function render():void
{
super.render();

renderGraphic(glare);
renderGraphic(label);
}


This process is basically what we’d do if we wanted to add additional elements to our button. Another strategy is to have a Graphiclist property instead of a Stamp, and add the custom graphics there. Then we can do the renderGraphic call with that Graphiclist.

The only thing left to do now is to populate the drawLabel function. This function will basically create a text field, set some format to it (font size: 20, custom font, white text, center alignment), set the text to it and apply a Glow Filter. Then it draws the label to a stamp, and centers the stamp vertically in our button.

		protected function drawLabel(text:String):void
{
var t:TextField = new TextField();

var tf:TextFormat = new TextFormat("Comfortaa", 20, 0xFFFFFF);
tf.align = "center";

t.defaultTextFormat = tf;
t.width = width;
t.height = height;
t.embedFonts = true;
t.text = text;
t.filters = [new GlowFilter(0x6ACFFF, 0.75, 7, 7, 2, 3)];

label = drawStamp(width + 10, t.textHeight + 10, 5, 5, t);
label.y = (height - t.textHeight) * 0.5 - 8;
}


We also need to embed the font. I used the bold version of the Comfortaa font face, which you can get for free here: Comfortaa at DaFont.com. After saving it in the fonts folder of our assets folder, we embed it from our Main class (the document class, the one which extends FlashPunk’s Engine):

	public class Main extends Engine
{
[Embed(source = "../assets/fonts/Comfortaa-Bold.ttf", fontFamily="Comfortaa", embedAsCFF="false")] public static var COMFORTAA:Class;

public function Main():void
{
super(550, 400);
}

//[...]
}


And here’s the end result!

## Step 3.10: Sound Effects

This one isn’t recommended for all of your UI elements, as it might get quite annoying to the player, but it could work in some cases. I’m talking about sound effects. Basically, we’re going to learn how to play a sound effect for specific case changes. To play the sound effects, we’ll use FlashPunk’s SFX class, which makes it really easy.

First of all, download the sfx. They are not that good, but they are OK for learning purposes. I made them using as3sfxr. Save them to the sfx folder of our assets folder, using right-click > “Save as…”

Then we need to embed them, in our Assets class.

		[Embed(source = "../assets/sfx/click.mp3")] public static const CLICK:Class;
[Embed(source = "../assets/sfx/out.mp3")] public static const OUT:Class;
[Embed(source = "../assets/sfx/over.mp3")] public static const OVER:Class;


In order to determine when to play those SFX, we’ll use the changeState function. We can add them inside the state switch statement.

			switch(state)
{
case NORMAL:
TweenLite.to(this, duration, { hexColors: { topColor: 0x6ACFFF, bottomColor: 0x005F8C, borderColor: 0x00456A }, onUpdate: updateGraphic } );
if (lastState == HOVER) new Sfx(Assets.OUT).play();
break;
case HOVER:
TweenLite.to(this, duration, { hexColors: { topColor: 0xFF8C66, bottomColor: 0xDD0500, borderColor: 0x550000 }, onUpdate: updateGraphic } );
if (lastState == NORMAL) new Sfx(Assets.OVER).play();
break;
case DOWN:
TweenLite.to(this, 0.1, { hexColors: { topColor: 0x5B5B5B, bottomColor: 0x2D2D2D, borderColor: 0x151515 }, onUpdate: updateGraphic } );
new Sfx(Assets.CLICK).play();
break;
}


As you can see in the normal and hover states, we can also check the last (previous) state. This is useful if, for example, we want our OVER sound to just play when we’re over the button. If that check wasn’t there, every time we clicked the button it’d also sound.

Here’s the result:

## Step 3.11: More Effects!

The possibilites are endless. We could add extra animations, particle candy, and all sorts of things to make our UI more fancy. Unimaginable ammounts of crazy features. Unfortunately, it’d be too long to cover all of them, and at this point you already know how to do stuff with our UI buttons and make it work with the skin. So, now it wouldn’t be a matter of “how to make x work with the buttons”, but just simply “how to make x thing” (like how to make particles), and that’s outside the scope of this tutorial.

Despite that, the example and the source include a variety of skins which introduce different techniques. You are free, and encouraged, to take a look at the commented source for each skin. This way you can learn more specific techniques. We’ll talk more about the source at the end of the tutorial.

## Step 3.12: Pixel-Perfect Detection

Even though I mentioned that I already explained enough effects and graphical techniques, there are still two behaviours you need to know. The first one is pixel-perfect detection.

That isn’t really noticeable now, but let’s make a test. Let’s change the radius of our button. It’ll look ugly, but it’s just for testing purposes.

		protected function drawButton(bitmap:BitmapData, width:Number, height:Number, topColor:uint, bottomColor:uint, borderColor:uint):void
{
g.drawRoundRect(0, 0, width, height, 100);
g.endFill();

sprite.filters = [new GlowFilter(0xFFFFFF, 1, 10, 10, 1, 3, true), new GlowFilter(borderColor, 0.6, 2, 2, 6, 2), new DropShadowFilter(8, 45, 0, 0.8, 10, 10, 1, 3)];

_m.tx = 5;
_m.ty = 5;
bitmap.draw(sprite, _m);
}


We changed the radius of our button from 20 to 100. Let’s see how it looks, and more importantly, how our button behaves.

Hitbox collision.

Everything, appart from the obvious hideousness of our button, looks normal. There’s one thing, though. Go place your mouse at the corners of our buttons. If you pay attention, you’ll see the mouse is detected even if it’s not over the button. That’s because we’re using hitboxes, so the graphic of the button isn’t taken into account – just its bounding box. And, of course, the mouse is inside that bounding box.

A way to solve that is to use FlashPunk’s Pixelmask. The pixelmask works like this for detecting collisions: first, it checks whether it’s inside the bounding box, just like the hitbox we’ve been using. If it’s not, okay, it’s fine. But, if it is, then it checks with pixel-precision whether it’s colliding with a graphic we provided. This way, we can make holes and rounded corners and all kinds of artefacts in our graphics, and it will only react to the cursor when it’s actually over the button.

There’s one thing, though. Imagine a button which only displays its border and the text, but is empty inside. We’d still want to detect the mouse when it’s inside the button, even if it’s on an “empty” part of it. Or, let’s say, as it happens with our case, that we have outter effects in our button, like a shadow. We don’t want to detect if the mouse is over those!

So, usually, a pixelmask is a different graphic than the button itself. In our case, the pixelmask would be just the rounded rectangle.

This will be easy to do. For the pixelmask, we’ll draw a black rounded rect. Then, we draw it on a BitmapData, and finally set our button mask to a Pixelmask of that BitmapData. Like this:

		protected function drawMask():void
{
g.clear();
g.beginFill(0x000000);
g.drawRoundRect(0, 0, width, height, 100);
g.endFill();

var bd:BitmapData = new BitmapData(width, height, true, 0);
bd.draw(sprite);

}


Remember to call the drawMask method in the constructor of the button! This technique will work with all your skins, just remember to draw the basic shape there.

Here’s how it looks:

Pixel perfect collision.

And here’s the pixelmask but with our 20 pixel radius. It’s barely noticeable, but the pixel-perfect collision is there!

## Step 3.13: Mouse Change

Another important thing to do is to change the mouse cursor. That’s important feedback for the user. The ideal behavior is to have a normal arrow cursor by default, but then have a hand cursor when hovering over a button. This way, the user knows that element is clickable. That’s almost always a recommended feedback (except for games like spot-the-difference, some kinds of graphic adventures, and so on).

To do that, we can use a FlashPunk property in the Input class: mouseCursor. It’s a string, and you can change it as often as you want without any glitches occurring, as the property makes sure only one change is made to AS3 at the end of the frame.

To achieve the effect, we need to set the cursor to “normal” first, and then set it to “hand” when the button detects it’s over it. Unfortunately, the only way we can do that is add a line to our World. If you don’t feel like adding that line to every world you create, you can either (a) modify FlashPunk or (b) create a BaseWorld class which extends World, add that line and make every world you create extend BaseWorld.

This is what we’ve got to do, in our update, before the super.update:

		override public function update():void
{
Input.mouseCursor = MouseCursor.AUTO;

super.update();
}


Now this needs to go on Button update. I repeat, on Button update, not on the button skin, which is BlueButton.

		override public function update():void
{
super.update();

if (collidePoint(x, y, world.mouseX, world.mouseY))
{
Input.mouseCursor = MouseCursor.BUTTON;

if (Input.mousePressed) clicked = true;

if (clicked) changeState(DOWN);
else changeState(HOVER);

if (clicked && Input.mouseReleased) click();
}
else
{
if (clicked) changeState(HOVER);
else changeState(NORMAL);
}

if (Input.mouseReleased) clicked = false;
}


Here’s the result:

## Step 3.14: That’s It!

Finally. We finished our first skin, a button skin, with complete animation and other handy techniques! Now you’re ready to skin all the buttons you want! But we must learn how to apply our knowledge to other components like checkboxes and radio buttons first…

## Step 4.1: Skin the Checkbox

Before we introduce the new things we will have to consider for the checkbox, let’s apply what we know about the button to the checkbox.

As I would have to explain everything again, and I don’t want to repeat myself too much, here’s our BlueCheckbox code, with comments when I think it’s needed to explain something. Also, I didn’t add the sound effects, as I find the ones I made too annoying:

package blue
{
import com.greensock.TweenLite;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.filters.BevelFilter;
import flash.filters.GlowFilter;
import flash.geom.Matrix;
import flash.text.TextField;
import flash.text.TextFormat;
import net.flashpunk.FP;
import net.flashpunk.graphics.Image;
import net.flashpunk.graphics.Stamp;
import ui.Checkbox;

public class BlueCheckbox extends Checkbox
{
protected var glare:Stamp;
protected var label:Stamp;

protected var buttonBmp:BitmapData;

public var topColor:uint = 0x6ACFFF;
public var bottomColor:uint = 0x005F8C;
public var borderColor:uint = 0x00456A;

public function BlueCheckbox(x:Number = 0, y:Number = 0, text:String = "", width:Number = 200, height:Number = 50, callback:Function = null, params:Object = null, checked:Boolean = false)
{
//we need width to always be larger or the same as height, because of the check square.
if (height > width) width = height;

super(x, y, text, width, height, callback, params, checked);

buttonBmp = new BitmapData(width + 20, height + 20, true, 0);
drawButton(buttonBmp, width, height);

drawGlare();
drawLabel(text);

graphic = new Stamp(buttonBmp, -5, -5);

}

{
//the mask is basically the check square + a square the size of the text.

g.clear();
g.beginFill(0x000000);
g.drawRoundRect(0, 0, height, height, 20);
g.endFill();
g.beginFill(0);
g.drawRect(height, label.y + 5, label.width + 5, label.height - 5);
g.endFill();

var bd:BitmapData = new BitmapData(width, height, true, 0);
bd.draw(sprite);

}

protected function drawLabel(text:String):void
{
var t:TextField = new TextField();

var tf:TextFormat = new TextFormat("Comfortaa", 20, 0xFFFFFF);

t.defaultTextFormat = tf;
t.width = width - height - 5; //take text position into account
t.height = height;
t.embedFonts = true;
t.wordWrap = true; //if our checkbox widht is smaller than the text, the text adapts.
t.text = text;
t.filters = [new GlowFilter(0x333333, 0.6, 6, 6, 4, 2)];

label = drawStamp(t.textWidth + 10, t.textHeight + 10, 5, 5, t);
label.y = (height - t.textHeight) * 0.5 - 8;
label.x = height + 5;
}

protected function drawGlare():void
{
//the same method from blue button, but instead of taking width into account, we use the height for the width of the rectangle,
//this way we make it a square. it will be the check square.

sprite.filters = [];
g.clear();
g.beginFill(0xFFFFFF, 0.25);
g.drawEllipse(-height * 0.25, -height * 0.6, height * 1.5, height);

glare = drawStamp(width, height * 0.4);
}

protected function drawButton(bitmap:BitmapData, width:Number, height:Number):void
{
//the same method from blue button, but instead of taking width into account, we use the height for the width of the rectangle,
//this way we make it a square. it will be the check square.
g.clear();
g.drawRoundRect(0, 0, height, height, 20);
g.endFill();

sprite.filters = [new GlowFilter(0xFFFFFF, 1, 10, 10, 1, 3, true), new GlowFilter(borderColor, 0.6, 2, 2, 6, 2), new DropShadowFilter(8, 45, 0, 0.8, 10, 10, 1, 3)];

_m.tx = 5;
_m.ty = 5;
bitmap.draw(sprite, _m);
}

override protected function changeState(state:int = 0):void
{
if (state == lastState) return;

var duration:Number = 0.25;
if (lastState == DOWN) duration = 0.15;

switch(state)
{
case NORMAL:
TweenLite.to(this, duration, { hexColors: { topColor: 0x6ACFFF, bottomColor: 0x005F8C, borderColor: 0x00456A }, onUpdate: updateGraphic } );
break;
case HOVER:
TweenLite.to(this, duration, { hexColors: { topColor: 0xFF8C66, bottomColor: 0xDD0500, borderColor: 0x550000 }, onUpdate: updateGraphic } );
break;
case DOWN:
TweenLite.to(this, 0.1, { hexColors: { topColor: 0x5B5B5B, bottomColor: 0x2D2D2D, borderColor: 0x151515 }, onUpdate: updateGraphic } );
break;
}

super.changeState(state);
}

protected function updateGraphic():void
{
buttonBmp.fillRect(buttonBmp.rect, 0);
drawButton(buttonBmp, width, height);
}

override public function render():void
{
super.render();

renderGraphic(glare);

renderGraphic(label);
}

private var _m:Matrix = new Matrix;
}
}


## Step 4.2: Determining the Check State

We need to determine whether a checkbox or a radio button is checked or unchecked, to make the skin go with that. For the checkbox, overriding the click method would do, but that’s different with the radio button, as the property gets changed from the radio button group as well.

What we’re going to do instead, is make the checked property a getter / setter, and call a changeCheck function when it gets changed.

First of all, make the variable protected and add an underscore. That’s the common practice for getters and setters. Then, generate its getter and setter, like this:

		protected var _checked:Boolean = false;

		public function get checked():Boolean
{
return _checked;
}

public function set checked(value:Boolean):void
{
_checked = value;
}


Now, just call the new changeCheck function when checked gets set, and it’s different from the original!

		public function set checked(value:Boolean):void
{
if (_checked != value)
{
_checked = value;
changeCheck();
}
}

protected function changeCheck():void
{

}


That’s it. Get ready for the actual skin code!

## Step 4.3: Checkbox’s Check

To code the check states, we’ll use a tick image. This tick image will be faded when clicking the checkbox, so it will display or hide with a fade in/out animation.

First of all, we need the check image. Download this one I made, and save it to the gfx folder of our assets folder. Then, embed it in the Assets class as CHECK.

Now, we’ll create the variable to store it. We’ll make it an Image, this way we can modify its scale and alpha properties easily. Name the variable check, and make it protected.

Next, we create it in the constructor. To scale it, we’ll get the ratio between the size of the check box (the square), using the height and the original width of the check image. This will make it as wide as the checkbox box, which will look nice as the image has a bit of blank space in the borders (basically because of the shadow). If it didn’t, we would have to adjust the formula a bit, by adding a value to the size (in this case, the height) so there would be some kind of border. We also set the alpha according to the checked property given to the constructor.

			check = new Image(Assets.CHECK);
check.smooth = true;
check.scale = height / check.width;
check.alpha = checked ? 1 : 0;


The next thing we need to do is render the check on our render function. We’ll check for the alpha property as well; this way we don’t waste resources by rendering an image with alpha 0!

		override public function render():void
{
super.render();

renderGraphic(glare);

if (check.alpha > 0) renderGraphic(check);

renderGraphic(label);
}


Finally, to determine when the button is checked and unchecked, we can use the changeCheck function we created on the previous step:

		override protected function changeCheck():void
{
super.changeCheck();

if (_checked)
{
TweenLite.to(check, 0.15, { alpha: 1 } );
}
else
{
TweenLite.to(check, 0.15, { alpha: 0 } );
}
}


That’s it for the checkbox! Check out how it looks:

To do the radio button, we’ll do the same as we do for the checkbox, but with a circular button and a circular check. The circular check will be drawn, and not an external imported image.

As we need to draw the check, but still modify its alpha, we’ll need a method to convert a sprite to an Image, just like the stamp method we have. I also added a method to give a BitmapData, which will come in handy when you need animated stuff, and don’t want to type it manually as we do in this tutorial.

		protected function drawStamp(width:Number, height:Number, offsetX:Number = 0, offsetY:Number = 0, sprite:DisplayObject = null):Stamp
{
return new Stamp(drawBitmap(width, height, offsetX, offsetY, sprite), -offsetX, -offsetY);
}

protected function drawImage(width:Number, height:Number, offsetX:Number = 0, offsetY:Number = 0, sprite:DisplayObject = null):Image
{
var img:Image = new Image(drawBitmap(width, height, offsetX, offsetY, sprite));
img.x = -offsetX;
img.y = -offsetY;
return img;
}

protected function drawBitmap(width:Number, height:Number, offsetX:Number = 0, offsetY:Number = 0, sprite:DisplayObject = null):BitmapData
{
if (sprite == null) sprite = this.sprite;

var bd:BitmapData = new BitmapData(width, height, true, 0);
_m.tx = offsetX;
_m.ty = offsetY;
bd.draw(sprite, _m);

return bd
}


Here’s the code with that drawImage method in use:

package blue
{
import com.greensock.TweenLite;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.filters.GlowFilter;
import flash.geom.Matrix;
import flash.text.TextField;
import flash.text.TextFormat;
import net.flashpunk.FP;
import net.flashpunk.graphics.Image;
import net.flashpunk.graphics.Stamp;

{
protected var glare:Stamp;
protected var label:Stamp;

protected var check:Image;

protected var buttonBmp:BitmapData;

public var topColor:uint = 0x6ACFFF;
public var bottomColor:uint = 0x005F8C;
public var borderColor:uint = 0x00456A;

{
if (height > width) width = height;

super(x, y, group, text, width, height, params, checked);

buttonBmp = new BitmapData(width + 20, height + 20, true, 0);
drawButton(buttonBmp, width, height);

drawGlare();
drawLabel(text);
drawCheck(); //as the check is a circle, it's easier to draw it than import an useless, circular image.

if (!checked) check.alpha = 0;

graphic = new Stamp(buttonBmp, -5, -5);

}

{

g.clear();
g.beginFill(0x000000);
g.drawRoundRect(0, 0, height, height, 20);
g.endFill();
g.beginFill(0);
g.drawRect(height, label.y + 5, label.width + 5, label.height - 5);
g.endFill();

var bd:BitmapData = new BitmapData(width, height, true, 0);
bd.draw(sprite);

}

protected function drawLabel(text:String):void
{
var t:TextField = new TextField();

var tf:TextFormat = new TextFormat("Comfortaa", 20, 0xFFFFFF);

t.defaultTextFormat = tf;
t.width = width - height - 5;
t.height = height;
t.embedFonts = true;
t.wordWrap = true;
t.text = text;
t.filters = [new GlowFilter(0x333333, 0.6, 6, 6, 4, 2)];

label = drawStamp(t.textWidth + 10, t.textHeight + 10, 5, 5, t);
label.y = (height - t.textHeight) * 0.5 - 8;
label.x = height + 5;
}

protected function drawGlare():void
{

sprite.filters = [];
g.clear();
g.beginFill(0xFFFFFF, 0.25);
g.drawEllipse(-height * 0.25, -height * 0.6, height * 1.5, height);

glare = drawStamp(width, height * 0.4);
}

protected function drawCheck():void
{
var d:Number = height * 0.7; // d = 7/10 of the button

sprite.filters = [];
g.clear();
g.beginFill(0xFFFFFF, 1);
g.drawCircle(d * 0.5, d * 0.5, d * 0.5);
g.endFill();

sprite.filters = [new DropShadowFilter(6, 45, 0, 0.8, 10, 10, 1, 3)];

check = drawImage(height, height);
check.x = (height - d) * 0.5;
check.y = (height - d) * 0.5;
}

protected function drawButton(bitmap:BitmapData, width:Number, height:Number):void
{
g.clear();
g.drawCircle(height * 0.5, height * 0.5, height * 0.5);
g.endFill();

sprite.filters = [new GlowFilter(0xFFFFFF, 1, 10, 10, 1, 3, true), new GlowFilter(borderColor, 0.6, 2, 2, 6, 2), new DropShadowFilter(8, 45, 0, 0.8, 10, 10, 1, 3)];

_m.tx = 5;
_m.ty = 5;
bitmap.draw(sprite, _m);
}

override protected function changeState(state:int = 0):void
{
if (state == lastState) return;

var duration:Number = 0.25;
if (lastState == DOWN) duration = 0.15;

switch(state)
{
case NORMAL:
TweenLite.to(this, duration, { hexColors: { topColor: 0x6ACFFF, bottomColor: 0x005F8C, borderColor: 0x00456A }, onUpdate: updateGraphic } );
break;
case HOVER:
TweenLite.to(this, duration, { hexColors: { topColor: 0xFF8C66, bottomColor: 0xDD0500, borderColor: 0x550000 }, onUpdate: updateGraphic } );
break;
case DOWN:
TweenLite.to(this, 0.1, { hexColors: { topColor: 0x5B5B5B, bottomColor: 0x2D2D2D, borderColor: 0x151515 }, onUpdate: updateGraphic } );
break;
}

super.changeState(state);
}

override protected function changeCheck():void
{
super.changeCheck();

if (_checked)
{
TweenLite.to(check, 0.15, { alpha: 1 } );
}
else
{
TweenLite.to(check, 0.15, { alpha: 0 } );
}
}

protected function updateGraphic():void
{
buttonBmp.fillRect(buttonBmp.rect, 0);
drawButton(buttonBmp, width, height);
}

override public function render():void
{
super.render();

renderGraphic(glare);

if (check.alpha > 0) renderGraphic(check);

renderGraphic(label);
}

private var _m:Matrix = new Matrix;
}

}


And the result:

## Step 5.1: Text Changed Callback

Now that we’ve skinned our Button, and its similar counter parts, the Checkbox and the RadioButton, it’s time to skin a TextInput.

First of all, we need to add some callback function for when the text is changed; this way we can change the text easily. This goes in the text setter.

		public function set text(value:String):void
{
if (_text != value)
{
_text = value;
changeText();
}
}

protected function changeText():void
{

}


## Step 5.2: The TextField

Time to make the skin itself. Create a new BlueTextInput under the blue package. There we will add all the skinning for our text input.

First of all, we need to create the TextField. We could’ve also added it to our TextInput base class, but maybe you’ll want to create some skin that uses bitmap fonts, or something else, so I decided to leave that to each skin.

package blue
{
import flash.text.TextField;
import flash.text.TextFormat;
import ui.TextInput;

public class BlueTextInput extends TextInput
{
protected var t:TextField;
protected var tf:TextFormat;

public function BlueTextInput(x:Number=0, y:Number=0, text:String="", multiline:Boolean=false, width:Number=150, height:Number=30)
{
super(x, y, text, multiline, width, height);

createTextfield(text);
}

protected function createTextfield(text:String):void
{

}
}

}


Now, let’s fill the createTextfield function! We’ll also need to make the text field drawable. We’ll use a stamp for that, as usual, and will make it update as well.

We’re offsetting the Stamp, so it will look right with the background:

		protected var t:TextField;
protected var tf:TextFormat;

protected var textBitmap:BitmapData;

public function BlueTextInput(x:Number=0, y:Number=0, text:String="", multiline:Boolean=false, width:Number=150, height:Number=30)
{
super(x, y, text, multiline, width, height);

textBitmap = new BitmapData(width, height, true, 0);

createTextfield(text);

graphic = new Stamp(textBitmap, 7, 5);
}

protected function createTextfield(text:String):void
{
textBitmap.draw(t);
}

override protected function changeText():void
{
super.changeText();

if (t)
{
t.text = _text;
updateTextfield();
}
}

protected function updateTextfield():void
{
textBitmap.fillRect(textBitmap.rect, 0);
textBitmap.draw(t);
}


The only thing left to do is fill the createTextfield function. We’re making a simple textfield, with no effects. (We’ll add fancy stuff later.)

		protected function createTextfield(text:String):void
{
tf = new TextFormat("Comfortaa", 20, 0xFFFFFF);

t = new TextField();
t.defaultTextFormat = tf;
t.embedFonts = true;
t.width = width;
t.height = height;
t.text = text;
t.multiline = this.multiline;
t.wordWrap = this.multiline;

textBitmap.draw(t);
}


Here’s two, non-really-visible, editable text fields. The first is single line, the second multiline.

## Step 5.3: Fancy Background

We need to see the text a bit better! That’s why we are going to code the background now.

As I would like to add some animation later to the background, to give visual feedback on a TextField control, we’ll use the same technique we used for the buttons to draw it.

First add this to the constructor:

			bgBmp = new BitmapData(width + 20, height + 20, true, 0);
bgStamp = new Stamp(bgBmp);

drawBg(width, height);


Then we need to make the drawBg function.

		protected function drawBg(width:Number, height:Number):void
{
g.clear();
g.drawRoundRect(0, 0, width, height, 20);
g.endFill();

sprite.filters = [new BevelFilter(10, 225, 0xFFFFFF, 1, 0x000000, 0.5, 6, 6, 0.5, 3), new GlowFilter(borderColor, 0.6, 2, 2, 6, 2),
new DropShadowFilter(8, 45, 0, 0.8, 10, 10, 1, 3)];

_m.tx = 5;
_m.ty = 5;
bgBmp.draw(sprite, _m);
}
private var _m:Matrix = new matrix;


Create the color properties as well, as we will tween them later:

		public var topColor:uint = 0x6ACFFF;
public var bottomColor:uint = 0x005F8C;
public var borderColor:uint = 0x00456A;


Don’t forget the render function. In this case, render the bgStamp before the super, so we render it below the text.

		override public function render():void
{
renderGraphic(bgStamp);

super.render();
}


Here’s the result. We can definitely see our text fields, now!

## Step 5.4: Focus Animation

I’d be cool to show some feedback to the user, so it’s easy to distinguish the selected text field.

We can override the onFocus function for that. Inside it, we’ll just need to tween the colors we are using in the drawBg method. Easy!

		override protected function onFocus(focused:Boolean):void
{
super.onFocus(focused);

if (focused)
{
TweenLite.to(this, 0.25, { hexColors: { topColor: 0xFF8C66, bottomColor: 0xDD0500, borderColor: 0x550000 }, onUpdate: updateGraphic } );
}
else
{
TweenLite.to(this, 0.25, { hexColors: { topColor: 0x6ACFFF, bottomColor: 0x005F8C, borderColor: 0x00456A }, onUpdate: updateGraphic } );
}
}


Don’t forget the updateGraphic function:

		protected function updateGraphic():void
{
bgBmp.fillRect(bgBmp.rect, 0);
drawBg(width, height);
}


That’s it. Check it out here!

## Step 5.5: Text Cursor

It would be nice to see some kind of marker at the end of the text. This is pretty easy to do. First, we need to build a Stamp with a white rectangle.

protected var cursor:Stamp;

			cursor = new Stamp(new BitmapData(2, 25, false, 0xFFFFFF));
cursor.visible = false;
moveCursor();


Then we need to render it:

		override public function render():void
{
renderGraphic(bgStamp);

super.render();

renderGraphic(cursor);
}


We’ll make it visible only when the TextField is focused.

		override protected function onFocus(focused:Boolean):void
{
super.onFocus(focused);

if (focused)
{
TweenLite.to(this, 0.25, { hexColors: { topColor: 0xFF8C66, bottomColor: 0xDD0500, borderColor: 0x550000 }, onUpdate: updateGraphic } );
}
else
{
TweenLite.to(this, 0.25, { hexColors: { topColor: 0x6ACFFF, bottomColor: 0x005F8C, borderColor: 0x00456A }, onUpdate: updateGraphic } );
}

cursor.visible = focused;
}


Finally, we need to move it every time we change the text.

		protected function updateTextfield():void
{
textBitmap.fillRect(textBitmap.rect, 0);
textBitmap.draw(t);

moveCursor();
}


Here’s the moveCursor function. First it gets the width and height of the last line. For the height, it also multiplies it by the number of lines, so we position it at the last one. Finally, we set the cursor coordinates to those. In this particular case, we offset the x by 10 and the y by 8. (These offsets are likely to vary with font, size, etc. and the best way to determine them is by testing manually. Feel free to adapt them!)

		protected function moveCursor():void
{
var lastChar:TextLineMetrics = t.getLineMetrics(t.numLines - 1);
cursor.x = lastChar.width + 10;
cursor.y = lastChar.height * (t.numLines - 1) + 8;
}


Here we have our text fields with cursors:

## Step 5.6: Mouse Cursor

You can change the mouse cursor to a text cursor easily, with just one line of code, as we already implemented what we needed in our world. You just need
to add this to the update of your TextInput class:

			if (collidePoint(x, y, world.mouseX, world.mouseY)) Input.mouseCursor = MouseCursor.IBEAM;


Here’s how it looks. Hover over the text inputs and your cursor will change!

## Section 6: Conclusion

That was it for this tutorial. We learned how to skin our components with different techniques, we added animations and other fancy stuff like sound effects. Remember there’s infinite things you can add to your UI to make it more pretty.

The most important thing, though, is that the UI is customized for each game. It needs to fit each game, its gameplay, the mood… if it doesn’t, there’s no point on making a custom UI! Also remember not to add too many effects, just add the needed ones – don’t go overboard.

As there are a lot of techniques you’ll only need for specific things you want to do for each UI, I can’t explain all of them in a tutorial. But I can show you some more. If you have a look at our demo, you will see there are some different skins, each requiring its own technique. I’d recommend you download the source, and investigate how each of them is made. I’ve commented the necessary bits!

Good luck. Thanks for reading, and hope you make an awesome UI for your FlashPunk game. Leave here a comment with your game if you make one!

Other parts in this series:«How to Make UI Components for FlashPunk Games

• Qbrainz

Cool ,especially Neon UI

really good tutorial

thank you very much

• Paulo

Simply WOW!

Thanks for that!

• Nick

Great tutorial! You can tell that you really spent a lot of time on it and we all thank you for that.
I have recently been looking into this area with FlashPunk and you really started me in the right direction.
Three questions:
1. Is there a way to make the mouse cursor move with the arrow keys?
2. Have you looked into the FlashPunk community project Punk.UI? They have posted a flash punk ui plugin to github that is worth taking a look at.
3. Why did you choose to use the greensock plugin for tweening colors and alphas when you are already using FlashPunk. FlashPunk has ColorTween which can do both and you could break the alpha out into a simple VarTween if you want to keep it simple.
Thanks again!