Beginning ActionScript 3.0 – Literal and Constant

bruce-256x256 Get confused with the data types? Eh, it seems that we introduced a little more types and too much concepts in a time. But, you don’t need to worry about it, you just need to know that there exist such things now, and you’ll get familiar with them after some practices.

Literal value

After talking about the data types, let’s turn around to the original code.

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

In this statement, we’ve already talked about the “var”, the variable name “msg” and the data type “String”, and there are still the equal sign and the string “Hello, ActionScript 3.0” left.

It’s the “Hello, ActionScript 3.0” time now :) .

In ActionScript, we may use 24 to assign an int type variable, use 3.14 to assign a Number type variable, or use true to assign a Boolean type variable. All these values, such as 24, 3.14 and true, we call them literal values in ActionScript.

Literal value examples

The literal values in ActionScript including:

Literal numeric value (enter the number directly):

  1. var someNumber:Number = 17.239;
  2. var someNegativeInteger:int = -53;
  3. var someUint:uint = 22;

Literal String value (surround the text with double quotation marks):

  1. var firstName:String = “George”;
  2. var soliloquy:String = “To be or not to be, that is the question..

Literal Boolean value (use the literal values true or false):

  1. var niceWeather:Boolean = true;
  2. var playingOutside:Boolean = false;

Literal Array value (wrap a comma-separated list of values in square brackets):

  1. var seasons:Array = ["spring", "summer", "autumn", "winter"];

Literal XML value (enter the XML directly):

  1. var employee:XML = <employee>
  2.  
  3. <firstName>Harold</firstName>
  4. <lastName>Webster</lastName>
  5. </employee>;
  6. //The above literal examples is from <flash_as3_programming>

According to the location where it stores and the way how to use it, the data can be dividing into three classes, the first one is variable; the second one is the above one, literal value; and the third one is constant.

Constant

From its literal meaning, you may know its role in ActionScript. As you guess, when you want to store something surely unchanged during your application’s life circle, you can use this type to store the values.

In my mind, constant type is widely use in programming languages. Actually, it’s a compiler mechanism; it protects the values you don’t want to change. If you try to change the values, you will get a compiler error as the following code shows.

clip_image002

How to declare a constant

Look at the picture above carefully, you can find the code:

  1. const DAY:int = 7;

It’s the way to declare a constant, just the same as declaring the variables :) eh, except we change the “var” into “const”.

Remember the CamelCase? It’s for naming the variables. But this is not suitable for the constants. For constants, we usually use all uppercases to name it. When we talk about the events, you will see the constants almost everywhere.

Remember, the constant value will get at evaluated at the compiling time, and the strict mode only allows a constant’s value to be assigned at initialization time, as the following code shows.

clip_image004

That’s all for this topic :)

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.

One Response to “Beginning ActionScript 3.0 – Literal and Constant”

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

Leave a Reply