There are many times when you want to listen to link events from TextFields in ActionScript 3. You might want to perform a certain task when the user clicks on a link and if so the new feature in ActionScript 3 that’s called “link events” come in handy.

This class describes how you can use link events:

package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.events.TextEvent;
public class LinkEventTest extends Sprite
{
public function LinkEventTest()
{
// Create a TextField
var txt:TextField = new TextField();
/* Due to the width of our column on swedishfika.com
we save our link in a separate variable. */
var ourLink:String = "<a href=\"event:Hello World!\">";
ourLink += "Trace \"Hello world!\"</a>";
// Add the link
txt.htmlText = ourLink;
// Add the listener
txt.addEventListener(TextEvent.LINK, onClick);
// Add the textfield to the display list
addChild(txt);
}
/* Is triggered when the user clicks on the link
we declared above. */
private function onClick(e:TextEvent):void
{
// Traces "Hello World!"
trace(e.text);
}
}
}

When the user clicks on the link the TextEvent.LINK event is triggered and the onClick-handler is executed and traces the string “Hello world!”.

This is a really neat new feature that solves a lot of problems!