Tutorial details:
Written by: Marx
Time: 20 Minutes.
Difficulty Level: Intermediate
Requirements: Flash 5.
Assumed Knowledge: HTML Textfields, User Defined Functions.

Download the source here. (Zipped format)

The purpose of this tutorial is to accustom people to activating ActionScripts from within HTML textfields. The first section outlines the basic methodology involved, the second is where we build our movie.

To invoke actions we use the HTML anchor tag. You cannot call ActionScript code directly from the tag; instead you call ActionScript functions. The syntactic format for this is:

<A realsrc="asfunction:myFunction" href="http://www.actionscript.org/dev/admin/asfunction:myFunction">Activate Function</A>

Parentheses after the function name are not permitted, notice how, in the above script, we omit the parentheses. You are allowed to pass one parameter to a function:

<A realsrc="asfunction:<em>myFunction,myParameter</em>" href="http://www.actionscript.org/dev/admin/asfunction:<em>myFunction,myParameter</em>">Activate Function</A>


Normally with functions myParameter would be in parentheses but in this case we separate the two with a comma.

Right then we've covered the basics so now lets build our movie:

1) The movie contains four layers:

a) Actions.
b) BodyMc.
c) Texfields.
d) Interface.

The 'Interface' layer is purely optional so I won't be referring to it again.

2) Place four textfields on your stage. Do the following from the Text Options panel:

a) Name them oneP, threeP, randC and hexV.
b) Select Dynamic Text from the drop down menu.
c) Check the HTML box.

3) Build the 'bodyMc' movie clip. The design of this clip is again totally optional; its purpose is purely to provide a visual response.
The 'bodyMc' contains a total of nineteen frames with keyframes at 3 frame intervals, to provide the impression of an up and down pressing movement.

Add the following script to frame 1 of our MC:

stop ();
function randColor () {
c = new Color(this);
r = (Math.random()*256);
g = (Math.random()*256);
b = (Math.random()*256);
c.setRGB(r << 16 | g << 8 | b);
_root.hexV="#"+(c.getRGB()).toString(16);
}
function activate (num) {
this.goToAndPlay(num);
}

The above script contains one stop action and two 'functions'.
The first function randomly changes the color of our MC and then outputs the hexadecimal value to the 'hexV' textfield.
The second 'function' activates our 'bodyMc' MC. It contains a parameter that allows us to set which frame we want our MC to 'go to and play'.

4) Back in the main timeline, drag an instance of your 'bodyMc' from the library to the stage and name it 'bodyMc'.

5) Now we'll add the actions that invoke the functions. Put the following actions in 'frame1' of the main timeline:

oneP = "<A realsrc="asfunction:bodyMc.activate,13" href="http://www.actionscript.org/dev/admin/asfunction:bodyMc.activate,13">One Press</A>";
threeP= "<A realsrc="asfunction:bodyMc.activate,1" href="http://www.actionscript.org/dev/admin/asfunction:bodyMc.activate,1">Three Presses</A>";
randC= "<A realsrc="asfunction:bodyMc.randColor" href="http://www.actionscript.org/dev/admin/asfunction:bodyMc.randColor">Random Color</A>";

Notice how we use the correct path when addressing the function.
Lines one and two activate the same function; the only difference between the two is the value of the parameter. The value here will represent the frame from which we want our MC to play.

Well there you have it. Hopefully this has stirred up the creative juices and maybe inspired a few to take it further. There's several areas I haven't covered, like passing more than one parameter or calling Javascript statements, but I think we've more than covered the basics.

Good luck.