How to Trigger ActionScript with Text Field Hyperlinks (AS2)
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!
June 24th, 2006 at 6:12 pm
When I use it and I need multiple parameters, I would define a custom function that would split the only parameter on commas, giving me essentially my arguments in an array. For example:
will make the root play when you click on it, because 234 represents Dec 31, 1969, which has alrady passed.
will NOT make the root play when you click on it, because 2151190071921 represents Mar 2, 2038 which has not occurred yet.
Though this doesn’t work well when one of your “parameters” is a string with commas in it… I guess you could choose another character for the delimeter.
Also, you can use asfunction in a textfile, and then load it into a html enabled textfield. Something like:
inside the text doc, myText.txt
Then in flash, something like:
June 25th, 2006 at 12:46 pm
Good thinking, NSurveyor! It’s funny, as soon as I published that one, the
Some of my best insights come to me when I finally stop thinking about it.
String.split()method occurred to me. But I didn’t log back in to update the article. Glad you posted your thoughts.July 27th, 2006 at 8:39 am
This is great, I really appreciate the article … it saved my ass this morning. =)
July 27th, 2006 at 9:03 am
Glad to hear it, Toby!
July 11th, 2007 at 9:22 am
Any idea about how this could be used with rollover events in text. I would like to be able to dynamically size my textfield which contains a bunch of words that when moused over should pop-up a MovieClip. Right now I place invisible buttons behind the text, but there has got a be a better way.
Any advice would be great. Thanks.
jase
July 18th, 2007 at 9:54 am
Jase,
For better or worse, hyperlinks in Flash’s version of HTML only respond to the equivalent of a
Button.onReleaseevent. It’s possible to style hyperlinks with CSS, so that they visually respond to rollovers, but those “quasi-event handlers” can’t be tasked with actually doing anything.Several months ago, I read an entry in the Adobe ActionScript forum (by kglad, I think) that suggested using movie clips in place of the hyperlinked words. That is, when you would normally have entered an
<a>tag, use an<img>tag instead, and set thesrcattribute of that tag to the Linkage identifier of a movie clip in your Library. That movie clip would simply be a symbol that wraps a very small text field with the desired word. Anidattribute in the same<img>tag will allow you to assign an instance name to the movie clip in question.I haven’t toyed with the concept myself, but it’s probably worth a shot.
September 26th, 2007 at 10:21 am
have you toyed with that tag solution yet? i’m lacking a little knowledge on that techinique and could use some insight into how to do it.
November 20th, 2007 at 9:57 pm
mike,
I have toyed with that tag idea a bit, actually. It’s been tough so far, because the
<img>tag in Flash seems to force a line break after itself. Not sure this is a usable approach, after all … but if I do figure something out, I’ll be sure to blog about it!