So you have an Actionscript 3.0 project and you want to add a simple text label that behaves like a standard HTML link with hand cursor on mouse over, underline, click events, etc.?
This should have been a fairly simple task, but it turns out to be a tricky one. To make a long story short, here's the bottom line:
private var lbl:TextField;
/* add the following to the constructor or some other function */
var btn:Sprite=new Sprite();
btn.buttonMode=true;
lbl = new TextField();
lbl.width = 120;
lbl.height = 20;
lbl.text = "My Label";
var newFormat:TextFormat = new TextFormat();
newFormat.color = 0x00FFFF;
newFormat.size = 14;
newFormat.bold = true;
newFormat.font = "Arial";
newFormat.underline = true;
lbl.setTextFormat(newFormat);
lbl.selectable = true;
lbl.mouseEnabled = false; btn.addEventListener(MouseEvent.MOUSE_OUT,onBrowseLblMouseOut); btn.addEventListener(MouseEvent.MOUSE_OVER,onBrowseLblMouseOver);
btn.addEventListener(MouseEvent.CLICK,onBrowseLbl);
btn.useHandCursor = true;
btn.addChild(lbl);
this.addChild(btn);
Then add the event handlers, for example:
private function onBrowseLblMouseOver(event:MouseEvent) : void
{
var newFormat:TextFormat = new TextFormat();
newFormat.color = 0x454545;
newFormat.size = 14;
newFormat.bold = true;
newFormat.font = "Arial";
newFormat.underline = false;
lbl.setTextFormat(newFormat);
}
Thursday, May 7, 2009
Subscribe to:
Posts (Atom)