How to Trigger ActionScript with Text Field Hyperlinks (AS2)

ActionScript 2.0 Flash

Flash makes it very easy to turn ordinary text fields into hyperlinks. Sure, it can be done programmatically with dynamic text fields and ActionScript, but it can also be done with plain vanilla static text fields:  just select a text field (or words in it) and enter the desired URL into the Property inspector.  It’s the field at bottom center of that panel — not labeled, but you’ll see “URL link” in a tooltip when you hover over it.

What isn’t immediately obvious is that you can also trigger functions with that URL link field.  Why would you?  Well, maybe you want something to spin around on the Stage before your hyperlink opens a URL, or heck, maybe you don’t want to visit a URL at all, but instead, send the timeline to another location.  Here’s how to do it. 

An answer, short and sweet

Use the asfunction protocol rather than http.  Select your text field and type the following into the URL link of the Property inspector:

asfunction:trace,Hello world!

When you test your SWF, click the text field to send the string “Hello world!” to the Output panel.  Simple as that.

Note:  Under normal circumstances, you would pass the string parameter “Hello world!” into a set of parentheses (()) after the function name — but here, no parentheses are present, and the string parameter is not quoted.  Of course, you can use the above technique with functions of your own, not just native functions like trace().

asfunction:gotoAndPlay,25
asfunction:myFunction,some parameter

How it works

The key is the asfunction protocol.  In computing, putting it very simply, a protocol governs the communication of data.  The Internet protocol suite, for example, defines dozens of them, including the well known http, ftp, and mailto.  ActionScript simply happens to support a proprietary protocol for triggering itself.

Unfortunately, asfunction it seems a bit limited.  I can only get it to accept one parameter after the function name … anything after that first comma, including other commas, seems to get included in the parameter.  But hey, at least it does something!  ;)

Leave a Reply