basic introduction into Flash scripting (programming in general)

i publish this in response to a request about a full list of commands etc.

you can find info about that at http://www.moock.org/webdesign/Flash/ or at
http://www.macromedia.com/support/Flash/ts/documents/Flash_websites.htm where you find a
list of Flash relatet pages. a lot of them should have samples.

however, the problem is usually not getting a list of commands (all commands are documented
in the manual or the online help, if you go into the expression editor you even find a list of all
functions etc) but to combine them to useful scripts if you don't have previous programming
experience.

if you don't have previous programming experience then here are a vew things which you should know and should help to get you onto the right track. however - the beauty of the scripting options is just the fact that it is not premade - it is a set of possibilities and up to your imagination to do exciting things with them.

for this page i created a simple sample file which should help to understand some basics of Flash (or other) programming.

click here to download the fla file.

the sample:

the following file contains a script that checks if the user enters at least 4 characters of text before s/he hits the button.

scripting basics:

- variables: a variable in programming is simply a container with a name and a
value. in Flash 4 you can create variables either by creating a textfield (the name of the textfield
is the variable name (we call it e.g var1) whatever is the current text of it is the variable value).
or by using the command Set Variable and define it like: var1 = 3 or var1 eq "three".

as you see there are two different kinds of variable content: expression values (3 which means
the value of the digit 3) and string literals ("three" or even "3" as real text and not as the
mathematical value 3).

- properties: as the name says, this is a property of an object. properties of movieclip instances would be e.g.: the related symbol, the instance name, the visibility, the x and y position on the stage, the scale factor, its rotation angle etc.

if you simply drag a movieclip onto the stage, position it and use the scale tools you basically define the properties for this instance. you can also use the command Set Property to redefine the property of an instance, e.g. you could have a button and define, that on Rollover a certain movieclip is set to invisible.

properties could have numeric values like an x position of 137 or simply be TRUE or FALSE like the visibility property (instead of TRUE you could as well use 1, instead of FALSE 0).


- functions: a function is an element in programming which allows you to retrieve certain properties or conditions etc. for instance if you ask the user to enter a password and want to make sure that it is at least 4 letters long, you can use the function Length to check how long the textstring is the user has entered.

if you want to make sure that the user enters only numbers you could use the function Ord to check the ASCII values of the entered text.

another example would be the Eval function. as stated before you can have expression or string literal variables. if you have a string literal variable with the value "3" you might have to retrieve the "mathematical" value of this before you can use it e.g. to multiply something by the value of this variable (you need a digit in this case, not text). the Eval function would do that.

- set and test / if statements
a key element of programming is setting and testing properties and variables. to set variables or
properties simply means to define them. testing means that you check what value a variable or a property has.

while all properties can be tested not all of them can be set. Flash allows you anyway more or less only to test for Flash internal properties, but other programming languages allow you e.g. to test for system properties. you might e.g. want to test if a user is on Mac or Windows or if s/he uses Netscape or IE. obviously these properties can not be set, you can not have a button which changes a user's system from Mac to Win, but you might use the information you get from the test to do certain things depending on the result.

the way to test for things is usually to use an if statement. this does not only allow you to test
for a certain value it also lets you define what should happen depending on the outcome of the check like in our sample:

If (Length (var)<4)
  Go to and Stop (2)
End If

in this case we have a variable(=textfield) with the name var. We want to make sure that the text entered into the textfield is at least 4 characters long. first thing we know is that we want to test a value, therefore we need an if statement which you get by selecting it from the + menue at the actions.

to be sure to get the right syntax for our statement it is probably best to use the expression editor. you click on the "expression" radiobutton then to be able to use the function field. we know that we want to check for the length of a textstring so we scroll to the Length function in the string sector and doubleclick it. it automatically comes up with Length(string).

the next thing is to tell it, which string should be checked, in our case it is the value of the variable var, so we replace the string with var. then we have to define what we test for - in our case we want to see if we have 4 characters or more, so we enter <4 (smaller than 4, if you e.g. want to have exactly 4 characters you could enter = 4)

that is the test finished. back in the actions dialog we have to tell Flash, what to do if the condition (Length (var)<4) is TRUE. therefore we click on the plus and select e.g. Go to and Stop (2)

we can also tell Flash what to do, when the condition is not TRUE by inserting an Else clause. therefore you highlite the if line on the left and click on the "Add Else/Else if Clause" button on the right. then you highlite the else line on the left, click on the + and insert e.g. Go to and Stop (3). the result should look like this:

If (Length (var) < 4)
   Go to and Stop (2)
Else
   Go to and Stop (3)
End If

thats it as far as the programming of this functionality is concerned. as it is you could use it as a framescript, the script will be executed when the particular frame is played. if you want it on a button you have to define another EVENT that triggers the execution of the script, like:

On (Release)
  If (Length (var) < 4)
    Go to and Stop (2)
  Else
    Go to and Stop (3 )
  End If
End On

you could just as well use On(Rollover) or On (Rollout).

 

download the fla file to see how the thing is put together.
bg

1