The Flash ActionScript Variable
How do you create a variable in ActionScript? The easiest way is to use the variable in your program. Flash will recognize that it has never encountered this variable before and will make it "official". Although this method works and is perfectly acceptable, most programmers find it helpful to maintain more control. One way is by defining a variable using the var keyword. For example, let's create a variable that we will call "container" and assign to it an initial value to 20. Here is the code. Let's take a look at it and then break it down.
container = 20;
Notice that both lines of the code above end with a semicolon. The semicolon ends a line of program code much like a period ends a sentence. If you do not use the period at the end of your sentences, no one will be able to tell when one sentence ends and the next one begins. It's the same with programming. Don't forget those semicolons.
Let's take a look at the first line of code. It begins with the var keyword which is followed by the name we want to give to our variable. It is telling Flash that we are creating a new variable and we are naming it "container".
The second line of code assigns an initial value to the variable. This code might look familiar to those of you who have studied algebra. In algebra, x = 1 means that "x is equal to 1". The equal sign means "equal to" in algebra. However in ActionScript, the equal sign means "assign the value". Therefore, the second line is telling Flash to assign the value of 20 to the variable named "container".
The two lines of code above can be condensed into one line.
var container = 20;
Copyright 2018 Adobe Systems Incorporated. All rights reserved. Adobe product screen shot(s) reprinted with permission from Adobe Systems Incorporated. Adobe, Photoshop, Photoshop Album, Photoshop Elements, Illustrator, InDesign, GoLive, Acrobat, Cue, Premiere Pro, Premiere Elements, Bridge, After Effects, InCopy, Dreamweaver, Flash, ActionScript, Fireworks, Contribute, Captivate, Flash Catalyst and Flash Paper is/are either [a] registered trademark[s] or a trademark[s] of Adobe Systems Incorporated in the United States and/or other countries.
Editor's Picks Articles
Top Ten Articles
Content copyright © 2019 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact Diane Cipollo for details.