The info in this document is specific to Flash 4. For Flash 5, see our new site: Flash5actionscript.com     

   Using Flash 4 actionscript: an example



The Flash movie on the left is an example of using different methods to move an object back and forth. One is done with a tween, the second is done via a fixed actionscript loop, and the third is done with a user-controlled actionscript loop.

Movement with a tween can be controlled by varying the number of frames in the tween, or by varying the start and ending position of the ball. With actionscript, movement may be controlled by changing the number of iterations in the loop, changing the variation in x-position (or y-position) on each iteration, or by setting the start and stop positions of the object in script.


  An Flash 4 actionscript reference of sorts (examples and explanations):
   Set a variable (examples of assignment statements)

  Set Variable: "i" = 1
  Set Variable: "dir" = "right"
  Set Variable: "isdone" = true
  Set Variable: "j" = j + 1
  
The variable name on the left (assignment) side must have quotes around it. The value assigned to the variable should have quotes if it's a string of alphanumeric characters. Otherwise, if the value being assigned is a number, a logic (true/false) value, or a variable name (even the same variable name), it should not have quotes, as in the examples.

   Check a variable (example conditional statements)

  if (i > 1)
  if (dir eq "right")
  if (isdone = true)
  
For comparisons, the variable name should not have quotes around it. If a number variable is being checked, the operators are =, <, >, <=, >=. If strings are being checked, the operators are eq (equal) or ne (not equal). Flash automatically adds parentheses around the condition being checked.

   Referring to variables with a path

Variables exist only in the movieclip (timeline) in which they are defined, unless they are defined in the main timeline, in which case they are available globally.

To set a variable in a different timeline or level from the current one (the one that's doing the defining), a path must be specified. The path includes a level (if omitted, =level0, the main timeline), a movieclip name (again, refers to the main timeline if omitted), and nesting information, such as:

Set Variable: "/:i" = /:i + 1

   increment variable i on the main timeline
  / indicates the main (root) timeline
  : indicates that a variable name follows

Set Variable: "dir" = "right"

   set variable dir in the current timeline (movieclip) to "right"

Set Variable: "/march:isdone" = true

   set variable isdone in movieclip march to true
  /march refers to movieclip march on the main timeline

Set Variable: "_level2/march/newclip/:j" = 32

   set variable j, which is in movieclip newclip, which is nested in movieclip march, which is in a movie loaded into level2, to 32

   Example of a loop: this one executes the same action 4 times

  Set Variable: "i" = 1
  Loop While (i <= 4)
    (do something here)
    Set Variable: "i" = i + 1
  End Loop

  
Note that the variable on the left side of an assignment must be quoted, and must not be quoted on the right side. No quotes around the variable in the loop statement either.

   Changing an object's size, position or transparency

[Example 1: shrink object by 50%]:
  Set Property ("/mcinstance", X Scale) = 50
  Set Property ("/mcinstance", Y Scale) = 50

[Example 2: move object 5 pixels to the right]:
  Set Property ("/mcinstance", X Position) = GetProperty("/mcinstance",_x) + 5

[Example 3: rotate object 30 degrees counterclockwise]:
  Set Property ("/mcinstance", Rotation) = -30

  
One way to control an object's size, position, or transparency in actionscript is to convert that object to a movieclip (because movieclips are the only objects in Flash to which you can individually assign properties with actionscript). Draw the object, select it, press ctrl-F8 to turn it into a symbol, choose movieclip, give it a symbol name, right-click the newly created symbol, click Properties, and assign an instance name to it. Property assignments only work on instance names, not symbol names.

To find one of the current properties of an instance of a movieclip, use the GetProperty function. To set or change that property, use the Set Property command, as in the examples at left.

   Changing the properties of all objects in a frame

  Set Property ("", Alpha) = 30

  
Because no movieclip instance is specified, everything (all objects, even non-movieclip objects) in the frame where this action is applied will be set to 30% transparency.

   Change transparency on entire frame: a working example




  
The example at left is a 2-frame Flash movie. There are 4 objects in the first frame and a stop action on the frame. The button advances to the next frame, where the action is Set Property ("", Alpha) = 30 (and then Stop).

Another statement was added to frame 1 (Set Property ("", Alpha) = 100) to reset the alpha on frame 1 when the change back button is clicked. It appears that text is not affected by a set property("", alpha) statement.

   3-frame loop to change a movieclip's property multiple times (example)

  Frame 1 (no label):
    Set Variable: "xpos" = 1

  Frame 2 (label=repeatloop):
    Set Property ("mcinstance", X Position) = xpos

  Frame 3 (no label):
    Set Variable: "xpos" = xpos+1
    Go to and Play ("repeatloop")

  
Three frames are necessary for repeatedly setting a property of a movieclip. The first initializes variables for looping and/or conditions before looping. The second assigns a new value to the x-position. The third increments the loop variable and returns control to the second frame.

The movieclip instance has quotes around it in the Set Property statement, and this must be an instance name, not the symbol name of the movieclip. (This is noted because it's very easy to create a movie clip symbol without assigning an instance name to it--make sure you do before using any Set Property statements).

   Jump to a frame using variables

  Example 1: Assign the frame name to a variable
    Set Variable: "gostring" = "repeat"
    Go to and Play (gostring)

  Example 2: Modify the frame name
    Set Variable: "modifier" = "2"
    (or Set Variable: "modifier" = 2)
    Go to and Play ("scriptmove"&modifier)

  Example 3: Assign the frame number to a variable
    Set Variable: "framenumber" = 42
    Go to and Play (framenumber)

  
When jumping to a frame, the expression used must evaluate to either a frame name or a frame number. It cannot be a frame name plus some number of frames, for example.

A modified frame name should be specified as in example 2, not using eval.

   Array loops

  Set Variable: "j" = 1
  Loop While (j<6)
    Set Variable: "p"&j = eval("ncount"&j)/ntotal * 100
    If (eval("p"&j)-int(eval("p"&j)) > 0.5)
      Set Variable: "p"&j = eval("p"&j)+1
    End If
    Set Variable: "p"&j = int(eval("p"&j))
    Set Variable: "j" = j+1
  End Loop

  
This example shows the syntax of various statements which use evaluated variable names. In this example, variables p1 to p5 are being set to a percent value based on the values of ncount1 to ncount5.

(As an aside, this example also shows how rounding can be done in Flash, since there is no round function, and the int function simply truncates).

   Notes on when to use "eval"

  Set Variable: "p"&j = eval("p"&j)+1

  Set Variable: "p"&j = eval("ncount"&j)/ntotal * 100

  If (eval("p"&j)-int(eval("p"&j)) > 0.5)

  
Eval should be used when a variable name is concatenated with another value (usually for doing array-like things--that is, the same action repeatedly--as in the example above) in those places in the statement when the variable name would normally not have quotes around it. Examples, as shown, include the right side of assignment statements (but not the left side), and anywhere within a conditional statement. To get the correct format, make sure you select Expression for the Value, and type the exact phrase you want into the Value box (for an assignment statement), or the Condition box (for a conditional statement).

   Last updated: April 23, 2000

  Please address any suggestions, comments or error reports to Helen Triolo at info@i-technica.com.

more whitestuff

Copyright © 2000 i-Technica