User Guide

Basics for working with the core display classes 185
{
upState = new TextButtonState(0xFFFFFF, txt);
downState = new TextButtonState(0xCCCCCC, txt);
hitTestState = upState;
overState = upState;
addEventListener(MouseEvent.CLICK, buttonClicked);
}
public function buttonClicked(e:Event)
{
trace("Button clicked.");
}
}
The hitTestState property of a SimpleButton object is a DisplayObject instance that
responds to the mouse events for the button. In this example, we set the
hitTestState
property (and the
overState property) to be the same DisplayObject instance as the upState
property. The TextButton class in the example references the following class, which defines
the DisplayObject to be used for a button state (up, down, or over):
import flash.display.*;
import flash.text.TextFormat;
import flash.text.TextField;
class TextButtonState extends Sprite
{
public var label:TextField;
public function TextButtonState(color:uint, labelText:String)
{
label = new TextField();
label.text = labelText;
label.x = 2;
var format:TextFormat = new TextFormat("Verdana");
label.setTextFormat(format);
var buttonWidth:Number = label.textWidth + 10;
var background:Shape = new Shape();
background.graphics.beginFill(color);
background.graphics.lineStyle(1, 0x000000);
background.graphics.drawRoundRect(0, 0, buttonWidth, 18, 4);
addChild(background);
addChild(label);
}
}