Wednesday 28 November 2012

Data types in ActionScript 3.0



In ActionScript 3.0, you can divide all data types into two sets. One is primitive value, and the other is complex value. The primitive values including “int”, “uint”, “Number”, “Boolean” and “String”; and the complex values including “Array”, “Data”, “Function”, “RegExp”, “XML”, “XMLList” and “Error”. Eh, you just need to remember the primitive values, except them are all complex values.

Primitive values

int
Remember int is short for integer, so int is use for storing integer value. In mathematics, the range of integer is from infinitely small to infinite, the range is infinite. But the memory we use to represent an integer is limit, so we can’t express all the integers with the limited memory. Actually, we use 32-bit to represent an integer in ActionScript 3.0, which means the range of integer we can represent is from -2^31 to 2^31 – 1.
Why the base is 2?
That because our computer can only recognize two digits, one and zero, is a binary system.
Why the exponent is 31, not 32?
That’ because we use one bit for sign, then the remaining bits is 31-bit, which use for representing the actual value.
Why the range seems asymmetric?
It seems that the integer values are asymmetric. That’s because this language use two’s complement to represent the integer values. You can just simply consider that’s all because we use one position for zero, so it needs to minus one 

uint
uint is short for unsigned integer. That’s to say, the sign-bit is disappear, we use all 32-bit to represent the integers. And no sign-bit, no negative integers. So, as you think, the range is from 0 to 2^32 – 1.
In most cases, the int type is enough for you to represent an integer. But there is still some situations we need to use this type, uint. Such as, you can use uint to represent some integer surely not less than 0, or to represent a color. Remember int is not suitable for processing the color, use uint instead.

Number
We’ve introduced two data types to represent the integers. How to represent the floating-point number? Of course, there is a corresponding type for the floating-point numbers. It’s Number.
Number type uses 64-bit to store the floating number with the IEEE-754 standard. In this standard, 1-bit for sign, figuring out whether the number is positive or negative; 11-bit for exponent; and the left bits (52-bit) are use for the mantissa.
And the range you get the range from the static properties stored in the Number class, Number.MAX_VALUE and Number.MIN_VALUE.
You can use Number to store some integer values. The Number data type uses these 52 bits and a special hidden bit to represent integers from -9,007,199,254,740,992 (-253) to 9,007,199,254,740,992 (253).
According to the official document, we should use Number data type for integer values larger than the 32-bit int and uint types can store or for floating-point numbers to maximize performance.

Boolean
You just need to know there are only two values: true and false in this type. No other values are valid for variables of Boolean type.

String
As you see in the first section, the String data type represents a sequence of 16-bit characters. Someone has said that,”The programmers spend almost 60% of their coding time dealing with the strings”, so, String type is very important, we’ll talk about it later.

Null
There is only one value, null, in this data type. This is the default value for the String data type and all classes that define complex data types.

Null data type represents lack of data. A variable can have a null value in a variety of situations.
  • To indicate that a variable has not yet received a value
  • To indicate that a variable no longer contains a value
  • As the return value of a function, to indicate that no value was available to be returned by the function
  • As a parameter to a function, to indicate that a parameter is being omitted

void
Just like Null data type, there is also only one value, undefined, in this data type. Notice that, void data type can’t use for declaring variables, but you can use it for functions.



MovieClip

Movie clips are symbols that can play animation in a Flash application. This is the only data type that refers to a graphic element. The MovieClip data type is an object of class MovieClip. By using the methods of MovieClip class we can control the symbols of MovieClip. To access the methods of MovieClip class we use the dot operator like any other objects. For example
mc.startDrag(true);
Where mc is the object of MovieClip class and startDrag is a method of MovieClip class.

Object
An object is an instance of a  class. It is a collection of properties and methods. Each property has a name and a value. To access a property of an object we use the dot (.) operator. For example, to access the name property of object site we use
site.name
We will look into the details of creating custom objects later.


Undefined
The undefined data type has only one value, undefined. It means that a variable hasn't been assigned a value.


Object type variables:-

var grade_list : Array  = new Array(); 
var sprite: Sprite = new Sprite(); 
var mc: MovieClip = new MovieClip(); 
var textfield:ColorPicker = new ColorPicker();
var object : Object = new Object();

No comments:

Post a Comment