ActionScript Programming/PartI/Chapter 3

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Data types[edit | edit source]

In this chapter we will learn how to use variables and data types. First lets talk about variables. Variables are very useful to keep data in them, but each variable has its data type. Below are listed data types available in ActionScript:

Data Type Description
String This is a text variable. For example if you set its value to "5" and then add 6 to it, the result will be "56" not "11". To have the result "11" you will have to use Number.
Number This is a number variable. If you want to add some numbers, then you must use this data type. But for example if you have a number variable which’s value is 5 and you add a string variable "5" to it then the result will be "55" not 10.
Boolean As we discussed in the previous chapters, Boolean is a data type which has only two values: true and false.
Date This is a date variable. You can keep your date in it or you can just access the current date (time, hours, minutes, year, month…).
Array This is a collection variable. You can keep on one but as much values as you want.
Object This data type is used to create or use objects.

General concepts and difference between Number and String data types[edit | edit source]

Lets write a small program to test this data types. Create a new movie and open Frame 1’s actions by right clicking the first frame in the Timeline window and choosing "Actions". In the Script pane type the following:

1.   var num = new Number();
2.   var txt = new String();
4.   num = 5;
5.   txt = "hi";
7.   trace(num);
8.   trace(txt);

The first line declares a new number variable: "num". The word "var" is used to declare variables. After "var" must follow the variable’s name, in our case it is "num". You can declare variables without assigning any data type. For example:

var i;

"var i;" means that we declare i variable without assigning any data type. In in our code "var num = new Number();" we declare "num" variable and assign "Number" data type. This means that "num" variable can keep and work with only numbers. The word "Number" and other data types must be followed with the brackets. If there is nothing written in the brackets then the value of the variable is set to its default. In our case, our num variable is 0 at first. If you delete the line 4, you will notice, that the traced value is 0. In line 2 we declare another variable "txt" but this time we assign it "String" data type. This means that "txt" variable will not be able to calculate or simply work with numbers. In line 4 we set the value of "num" to 5 and in line 5 we set the "txt" value to "hi". And finally when we trace these two variables in lines 7 and 8 we saw the output:

Mfas image011.jpg

Now lets make a test.

1.   var num1 = new Number(20);
2.   var txt1 = new String("Hello");
3.   var num2 = new Number(5);
4.   var txt2 = new String("...");
6.   num1 += num2;
7.   txt1 += txt2;
9.   trace(num1);
10.  trace(txt1);

In first 4 lines we declare 2 number and 2 string variables and set their values. In lines 6 and 7 we calculate two same data type variables with each other and keep the result in the first variable. For example in line 6 "num1" variable is 20, by using "+=" operator we add "num1" "num2". We can replace the line with "num1 = num1 + num2". This is the same. If we add 5 to 20 the result will be 25. In line 7 we combine two strings with each other. The result is kept in "txt1" as in "Hello…". Now test the movie by pressing Ctrl + Enter and you will see the results 25 and "Hello…". Now lets test the same example but only change lines 6 and 7:

6.   num1 += txt2;
7.   txt1 += num2;

What do you think will happen? Test the movie and see. The variable "num1" will become a string, because the result will be:

Mfas image012.jpg

<< PreviousNext >>