AS Reference  :  Notes Index  :  Resources  :  About/Contact  :  Downloads

ActionScript Declaration and Assignment Statements


As we saw on the previous page, assignment statements can be used to assign values to variables. They can be combined with declaration statements to both declare a variable and assign it a value, or the declaration and assignment may be done separately. A variable is a named spot set aside in memory, of a particular type (class or primitive datatype), where values may be stored and retrieved as a Flash movie is executing. A variable can also be viewed as a property of a particular timeline (the timeline on which it is declared). Each timeline has its own set of variables, which are user-defined properties added to the built-in properties that every timeline (ie, instance of MovieClip class) already has. Starting with Flash MX 2004 (Flash 7), variables can and should be defined as being of a particular type when they are declared.

Declaration

The general form for such a declaration is:

Classname is capitalized because class names usually are. A couple examples:

  var addresses:Array;   // addresses is a variable which is an instance of the Array class
  var n:Number;          // n is a numeric variable (and so can only have numeric values)

These statements create variables of the specified type (class or datatype) in the timeline where this code is. They will be given values later in the program. Here, a marked spot in memory is set aside for each.

Declaration and Assignment

If a value is to be assigned to the variable when it is declared, then the general form is:

For example:

var id:String = "sac002";

This creates a variable id, of type String, on the current timeline, and assigns the string of characters, 'sac002', to it. That is the variable's initial value; it may changed to some other string value later in the program. If, later in the movie, you assign an obvious numeric value (without quotes around it to make it a string), you'll see an error in the Output panel.

var finished:Boolean = false;

Variable finished is created and set initially to false. Presumably it will be set to true by something later in the movie. Never put quotes around true or false when assigning values to Boolean variables or properties.

var zipcodes:Array = new Array('20852', '40938', '50392');

Variable zipcodes, an instance of the Array class, is created and assigned an initial value by passing parameters to it. The parameters in this case (whenever you create an Array instance) are the elements of the array. An array is a good way to store repetitive data -- more about arrays can be found here.

Assignment

If the variable has already been declared (and associated with a timeline, as it is automatically when it is declared), then the general form of the assignment statement is:

For example:

id = "mpx005";

This sets the value of variable id in the current timeline to "mpx005". It assumes that the variable id has already been declared as a variable of type String.

bee1_mc.stopped = true;

This statement assumes that a variable (property) stopped has already been declared for bee1_mc (or, if not, one is created when the statement executes). The value of true is assigned to bee1_mc's variable (property) named stopped. bee1_mc must be a movieclip in the same timeline as this code for this statement to work.

Setting the property of an instance to some value

As we saw in the last statement above, an assignment statement can be used to assign a value to a particular property of an instance.

Notice that properties themselves are always instances of a particular class.

Movieclip instances, for example, have built-in properties such as _alpha, _visible, and _xscale which can be read and set with actionscript. This is the general form for assigning a value to a property:

eg,

racecar_mc._alpha = 70;

This statement causes the movieclip named racecar_mc, located in the same timeline and same frame where the statement is, to be set to 70% opacity. If the statement exists in a frame of the main timeline where the racecar_mc is not also present, it will have no affect. _alpha is a property which has a value of type Number assigned to it.

_parent.plane_mc._visible = false;

This statement causes the movieclip plane_mc, in the timeline above the one in which this code is written, to be invisible. Notice that the value assigned to the _visible property is false, with no quotes around it. _visible is a property which can only have a value of type Boolean (specified as either true or false) assigned to it (though you may see examples of a 1 or 0 being assigned to a Boolean property, which happens to work in Flash, because Flash automatically converts the number 0 to Boolean false, or 1 to true, before assigning it.)

Declaring functions, Using them in assignment statements

A function is also a kind of datatype; that is, when you declare or create a function in Flash, you're creating a variable of type function. A spot in memory is set aside which points to the code (the set of statements) included in the function. If the function is named, that name is associated with (points to) the function's spot in memory (the place where all of its code is stored). If it's not named, Flash still keeps a reference to it, so it can be assigned to something else, like a movieclip event handler. Here are examples of a function declaration and two different kinds of assignments, in both of which a function is assigned to a movieclip property:

// declaring (and defining) a function named makePictureWider

function makePictureWider() {
   picture_mc._xscale *= 1.5;
}

// assigning that function to a movieclip property

control_mc.onRelease = makePictureWider;

// assigning an unnamed function to a movieclip property
// because this is an assignment statement, it has a semicolon at the end
// its effect is the same as the two examples above combined

control_mc.onRelease = function() {
   picture_mc._xscale *= 1.5;
};

Intro
Flash: What & How
Example Sites
Create
Draw, Edit Shapes
Gradients
More Drawing Tips
Import
A Sample
Animate
Frames, Keyframes
Motion Tweens
More Motion Tweens
Shape Tweens
Masks
Control
Stop/Replay
Movieclips Intro
Movieclip Reference
Site Structure 1
Slideshow Movieclip
Contact Form
Scroll Resume
Preloader
Site Structure 2
Publish
Display Options
Player Detection
Optimize
AS 2.0 Basics
Intro to Syntax
Playhead Commands
Playhead Cmds 2
Coded Tween
onEnterFrame
Intro to Classes
Declare/Assign
Comments, Trace
Simple Data Types
Arrays & Objects
Code Blocks
Operators
Beyond Buttons
Code Structure
Toggle Controls
Group of Buttons
Drag and Hit
Distort Magnifier
Scroll Text
Bee Game
Dart Shooter
Sound Control
Easing Slider
Easing Slider 2
Components Intro
Timers & Delays
Dynamic Content
Intro
Drawing API
Create Text
Attach Movieclips
Easing Slider 3
Easing Slider 4
Load jpg/swf
Sliding Viewer
Preload swf
XML
Easing Slider 5
Server Comm
LoadVars (w/ PHP)
AS - PHP Lookup
Text File
Database 1:LoadVars
Database 2:Remoting
Read from directory
AS 2.0 Classes
Intro
Math
Key
Date
Color
EventDispatcher
New Samples
Pie Chart
Event-model Emailer
Tween Sequence
Fuse Sequence
SVG in Flash
Bitmap Topo
SWF as Data Holder
Two-level Menu
Yahoo! Flash Maps
Class-based Game
ASTB Samples
Disclaimer
3D Outlines
Bounce Collide
Address Book
Save Drawings
Home  :  Notes Index  :  Resources  :  About/Contact  :  Downloads