Beginning ActionScript 3.0 – Variables

Firefox-old-school-final-256x256 What we have done in previous section?

We draw a text field on the stage, and give it an instance name “dText”. And then we write down the following code.

var msg:String = “Hello, ActionScript 3.0″;
dText.text = msg;

Here, we define a String type variable named “msg” and assign the “Hello, ActionScript 3.0” to it. We visit the property “text” in “dText” with the dot operator “.”, and assign “msg” to the property. When you test the movie, you can see the string “Hello, ActionScript 3.0” shows on the text field. That’s all we done.

Now, let’s focus on the first statement.

var msg:String = “Hello, ActionScript 3.0″;

This statement is equals to:

var msg:String;
msg = “Hello, ActionScript 3.0″;

It’s a combination of two statements, the first one is a declare statement, and the second one is a assignment statement. Actually, the statement

var msg:String = “Hello, ActionScript 3.0″;

will become to:

var msg:String;
msg = “Hello, ActionScript 3.0″;

at the compiling time. This due to the technique called hoisting. This technical means that the compiler moves all variable declarations to the top. You just need to know there exist such a technical. We’ll talk about it later, eh, maybe with the scope.

OK, let’s just focus on the first statement:

var msg:String;

This is a var statement in ActionScript 3.0, which use for declaring a variable.

Why variables?

Variables allow you to store values that you use in your program. Just as we use the msg here, we use the msg to store a String type value, and then assign it to the dynamic text.

How to declare a variable?

Use the var statement. You can use the following statement to declare a variable named msg.

var msg;

In this statement, the keyword “var” is a must. If you omit the var statement when declaring a variable, you will get a compiler error in strict mode and run-time error in standard mode. For example, the following line of code will result in an error if the variable msg has not been previously defined:

msg;

This statement will cause an error, if you haven’t declared the msg previously.

About the variable name

In the statement:

var msg;

We declare an untyped variable which name is msg. The variable name is the identifier for the variable, which means, when you want to use the variable, use its name. You can get or set the variable’s value with its name. Just as

msg = “Hello, ActionScript 3.0″; // we set the msg’s value here
dText.text = msg; // we get the msg’s value here

That means msg is the identifier for the variable you’ve just declared. Through the identifier, you can access the variable.

Actually, you can treat the variable name as the variable itself. When we say the variable msg, it often means the variable whose name is msg.

The rules of identifier

The variable name is the identifier of the variable. In ActionScript, the identifier is everywhere. You need the identifier to access the classes’ instances, the variables, the functions and so on. So, it’s important to know some rules of defining the identifier.

1. The first character must be an uppercase or lowercase letter or underscore (_).

2. The remaining characters must be letters, digits, or underscore (_).

3. Names are case sensitive.

4. Can’t conflict with the intrinsic keywords

Here is some wrong identifiers, please help me to correct them :)

hey jude

4you

Memory#1

If you want something more about the identifier, you can take a look at the CamelCase, http://en.wikipedia.org/wiki/CamelCase ; it will help you choose the proper name for the identifiers.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

5 Responses to “Beginning ActionScript 3.0 – Variables”

  1. [...] the first ActionScript project Variables Data type Literal and [...]

  2. [...] Here is a very cool tutorial I found explaining some of the simpler things to do with actionscript. Click here for the tutorial! For more great actionscript related tutorial material check out the site: [...]

  3. alex says:

    hi,ncc! i translate this article into chinese. Specify the author and link。If you are‘t allow, I may revoke.
    My English is not good!

  4. oneLove says:

    Thank..

  5. [...] the first ActionScript project Variables Data type Literal and [...]

Leave a Reply