ActionScript Programming/PartI/Chapter 3/Number,Boolean,Date
In section General concepts and difference between Number and String data types you understood the difference between String and Number data types. Now we will analyze Number data type in details. The following is the structure of declaring Number variable:
var <name> = new Number( [initnumber] );
The parameter <name> is the name of the variable you want to declare, and the parameter [initnumber] is the value you want the variable to be assigned when it is created.
|toString||returns the String representation of the specified String object.|
|valueOf||returns the primitive value type of the specified Number object.|
This method returns the string representation of the specified String object. You already know the difference between Number and String objects so there is no need to explain why we need this method.
1. var i = new Number(555); 2. trace(i.toString());
This method returns the primitive value type of the specified Number variable (object).
Now lets test the Boolean data type. Add a new button or movie clip in the scene and set its’ name to “testobject”, then open the actions of Frame 1 and write the following in Script pane:
1. var show = new Boolean(false); 2. testobject._visible = show;
The first line declares a Boolean variable “show” and sets its’ value to “false”. In the third line we set “testobject’s” property “Visible” to the “show” variable we declared. If you test the movie you won’t see the “testobject”, because the show value is “false” but if you change the word “false” in line 1 to “true” then the object will be shown.
|toString||Returns the String representation (“true” or “false”) of the Boolean object.|
|valueOf||returns true if the primitive value type of the specified Boolean object is true, false if it is false.|
This method returns the string representation of the Boolean object. Now you ask why do you need to use this method if you can do without it by just writing for example: “x=x+true;”, where x is a String variable. To understand the use of “toString” method you must first understand the Boolean data type. Actually the Boolean data type is a masked number data type. The values “true” and “false” are numbers. “true” is 1 and “false” is 0. To test this, we can write a simple code:
1. var i = new Number(); 2. i = false; 3. i = i + 0; 4. trace(i);
If you run the program, you will see “0” in “output window”. Lets understand what we have done. First we declared a Number variable “i”, then assigned “false” to it. If you omit the third line, the output will be “false”. But what have we done? In line three we added 0 to variable “i”. Why have we done this? Because we wanted Flash to think that we added some number to variable “i” which would change the value of it. So it converted the stored “false” value to number and then added 0 to it. Briefly we made Flash convert the value “false” to number. If we write “true” instead of “false” in line 2, then you will see that the traced value is 1. This means that actually “true” and “false” values are numbers and correspondingly equal to 1 and 0.
This method returns true if the primitive value type of the specified Boolean object is true, false if it is false.
This is a very complicated data type, it has 37 methods. Now we will learn some of them.
1. var d = new Date(); 2. 3. trace("Date: " + d.toString()); 4. trace("Hours: " + d.getHours()); 5. trace("Minutes: " + d.getMinutes()); 6. trace("Seconds: " + d.getSeconds()); 7. trace("Milliseconds: " + d.getMilliseconds()); 8. trace("Year: " + d.getFullYear()); 9. trace("Month: " + d.getMonth()); 10. trace("Date: " + d.getDate()); 11. trace("Day: " + d.getDay());
The first line declares a new Date variable “d”. So now the date is stored there. In lines 3-11 we trace the values of our “d” variable.
|getDate||Gets day of month.|
|getDay||Gets day of week.|
|getFullYear||Gets full year.|
|getHours||Gets hour of day.|
|getMilliseconds||Gets milliseconds since last second began.|
|getMinutes||Gets minute of hours.|
|getMonth||Gets month of year.|
|getSeconds||Gets seconds of last minute began.|
|setDate||Sets day of month.|
|setDay||Sets day of week.|
|setFullYear||Sets full year.|
|setHours||Sets hour of day.|
|setMilliseconds||Sets milliseconds since last second began.|
|setMinutes||Sets minute of hours.|
|setMonth||Sets month of year.|
|setSeconds||Sets seconds of last minute began.|
|toString||Returns a string representation of the date/time|