View Single Post
Old 11-28-2006, 12:25 AM   #30
TheCanadian
Noo doot aboot it, eh?
 
TheCanadian's Avatar
Location Take a guess . . .

Posts 7,089
How can I use htmlText to call an ActionScript function?

This is done using an anchor tag and the asfunction protocol. It looks something like this:

asfunction:function:Function, parameter:String

Note that the parameter is optional.

The usage is fairly straight forward and there is a lot of information in the help files/LiveDocs but I'll go over some sample usages.

The Basics
Code:
this.createTextField("myTextField", 1, 10, 10, 0, 0);
this.myTextField.autoSize = true;
this.myTextField.html = true;
this.myTextField.htmlText = "<a href='asfunction:traceArgs,hello world!'>Press Me</a>";
function traceArgs(a:String):Void {
       trace(a);
}
Whenever you press the text, Flash will output "hello world!"

Passing Multiple Arguments
The asfunction protocol only allows you to pass one argument to the function, but this can be easily remedied by using a character as a delimiter and splitting the string argument inside the function.
Code:
this.createTextField("myTextField", 1, 10, 10, 0, 0);
this.myTextField.autoSize = true;
this.myTextField.html = true;
this.myTextField.htmlText = "<a href='asfunction:traceArgs,arg1, arg2, arg3, argN'>Press Me</a>";
function traceArgs(a:String):Void {
 var args:Array = a.split(", ");
 for(var i:Number = 0; i < args.length; i++) {
  trace(args[i]);
 }
}
Now when you press the text, Flash will output each intended argument as an independant element of the args array.

Calling Functions in Different Scopes
To call a function in another scope, simply add the path to and the instance name of the movie clip before the function call - as in any other AS code:
Code:
this.myObject = new Object();
this.myObject.method = function():Void {
 trace("called");
}
this.createTextField("myTextField", 1, 10, 10, 0, 0);
this.myTextField.autoSize = true;
this.myTextField.html = true;
this.myTextField.htmlText = "<a href='asfunction:myObject.method'>Press Me</a>";
Whenever you press the text, "called" is outputted.



And that's about all there is to know to call AS functions from TextFields.

__________________
Proud Montanadian
We tolerate living and breathing.

Name Brand Watches
TheCanadian is offline   Reply With Quote