< ActionScript Programming < PartI < Chapter 3

Data types

Number

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.

Methods

MethodDescription
toStringreturns the String representation of the specified String object.
valueOfreturns the primitive value type of the specified Number object.
toString

String.toString( );

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());

valueOf

String.valueOf( );

This method returns the primitive value type of the specified Number variable (object).

Boolean

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.

Methods

MethodDescription
toStringReturns the String representation (“true” or “false”) of the Boolean object.
valueOfreturns true if the primitive value type of the specified Boolean object is true, false if it is false.
toString

String.toString( );

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.

valueOf

String.valueOf( );

This method returns true if the primitive value type of the specified Boolean object is true, false if it is false.

Date

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.

Methods

MethodDescription
getDateGets day of month.
getDayGets day of week.
getFullYearGets full year.
getHoursGets hour of day.
getMillisecondsGets milliseconds since last second began.
getMinutesGets minute of hours.
getMonthGets month of year.
getSecondsGets seconds of last minute began.
setDateSets day of month.
setDaySets day of week.
setFullYearSets full year.
setHoursSets hour of day.
setMillisecondsSets milliseconds since last second began.
setMinutesSets minute of hours.
setMonthSets month of year.
setSecondsSets seconds of last minute began.
toStringReturns a string representation of the date/time
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.