What Are Data Types? Flash

In Flash 4, any data that you had in Flash movie was not “typed.” A data type is a classification or group to which a piece of data belongs. Some data types cannot be converted into other data types, but some can. To illustrate data typing, take this working example in a Flash 4 movie:

In this code example, we have two Flash 4 variables, path1 and path2. The values of these variables are string paths to two different Movie Clip instances, ballAnim and ballPath. In Flash 4, we could use path1 and path2 as the target paths in a Tell Target action. In Flash 5, this code would still work:

Because the tellTarget action is Flash 4-compatible, it can accept string data (for example, “_root.ballAnim.ballPath”) for a target path in Flash 5. However, if we try to use Flash 5’s new Movie Clip Object methods to control the targets, then path1 and path2 will not work:

Why? Because path1 and path2 have string values, Flash 5 does not “see” path1 and path2 as a reference to a real object in Action Script. If we change the code to:

then Flash 5 will see path1 and path2 as references to the ballPath and ballAnim Movie Clip Objects. At this point, you might be wondering how you can use the oldfashioned eval() statement to refer to dynamic Movie Clip paths. In Flash 4, you could do this:

In Flash 5, if you want a variable to point to a Movie Clip object, then the value of the variable must be an object type. To do the same thing in Flash 5 with Movie Clip methods, you would change the preceding code to:

By using eval(), Flash 5 will evaluate the string concatenation of “_root. ballAnim.ball_” + i, which will produce the final string of “_root.ballAnim.ball_1”. At this point, Flash doesn’t “know” that the string refers to a Movie Clip Object. When this is evaluated, Flash will change the string data into object data, referring to the Movie Clip instance _root.ballAnim.ball_1. But even the eval() function is a little dated for Flash 5. Earlier, we discussed the use of array access operators—the [ ]—to bypass the eval() function. As with variables, the array access operators can be used to refer to Movie Clip expressions:

This code will join the “ball_” string to the value of i, and look for ball_1 Movie Clip object on the ballAnim timeline. So far, we have seen two data types at work: strings and objects. If a variable’s value is in quotes, as in “_root.ballAnim”, then Flash 5 sees this value as a string. If the variable’s value is evaluated or referenced directly, as in _root.ballAnim, then Flash 5 sees this value as an object. Flash 5 has a built-in action, typeof, which tells you the data type of a value or an expression. We discuss this action later in this section. Before we look at typeof, let’s examine the five data types in Flash 5 ActionScript.

We’ve seen string data types throughout the Flash 5 Bible already. Anytime you have a value in quotes, it is typed as a string. If you have an expression that refers to string data types, then its data type will be a string as well. All of the following examples have a string data type:

If a variable has a string data type, then any of the String methods can be used with that data. For example, if you want to convert the case of all characters in the value of firstName to uppercase (turn “Frank” into “FRANK”), then you could do the following operation:

Here, the String method toUpperCase() converts any lowercase characters in a string value to uppercase characters. Likewise, you can extract specific information from a string. For example, if you wanted to find where a space occurs within a string, and return the value of the string from the point in the value to the end of the value, you could use the following code:

In the preceding code, the indexOf() method searches for the first occurrence of a space (“ “) within the string value for myVersion. indexOf(“ “) for myVersion will return the position (as a number, counting from left to right) of the space character. For this example, indexOf(“ “) will return a 9. Then, we add 1 to this value to the character position after the space. In our example, the tenth position of myVersion’s value is a “4.” Then, by using the slice() method, we can extract the rest of the string from the startChar value of 10. The –1 option tells Flash to continue all the way to the end of the string’s value, from the starting point of startChar. Note that in this example, the final value of myVersion is a string value of “4.71.”

A number data type is any value (or expression value) that refers to a discrete numeric value in Flash. A value must be typed as a number in order for it to work properly in mathematical operations. Note in the following code that the ¬ indicates a continuation of the same line of code. Do not insert this character into your actual code.

If this code was added to a Flash movie and tested, the following trace information would appear in the Output window: Obviously, this isn’t the answer we were looking for. Because myAge and future Years were specified as string values (with quotes), Flash simply concatenated (joined) the two string values as “27” + “5”, which is “275”. To see these values as numbers, we need to change the code to the following (note that the ¬ indicates a continuation of the same line of code; do not insert this character into your actual code):

Now, the values of myAge and futureYears appear as real numbers to Flash, and the mathematical operation will add the values of myAge and futureYears correctly.

The trace output will now read: I will be 32 years old in 5 years. You can convert string data values to number data values by using the Number() function. In our string example from the last section, we could convert the myVersion string value to a number value by adding this line of code:

myVersion = Number(myVersion);

So, we can now perform mathematical operations on the “4.71” value of myVersion, which is now simply 4.71.

There will be times when you will designate a variable’s value as either true or false. Variables that use true or false are said to have a Boolean value. Boolean values are useful for either/or situations, or when you need a toggle switch just like a light switch, which is on or off. In the code below, a variable named isLoading is initialized with a true value, but later switched to a false value when loading is complete:

This code could be placed on a Movie Clip instance (as Object actions). When the Movie Clip instance appears on the Stage, the load event will occur, and the Output window will display:

isLoading’s type = boolean

As the data type name implies, Movie Clip instances on the Stage have a data type of movieclip. You can check the data types of declared variables and objects with the typeof operator. Flash distinguishes Movie Clip Objects from other code-based objects so that you can more easily detect Movie Clip Objects in your code. The following variable value will be typed as movieclip:

path = _root.ballAnim;

As long as a physical Movie Clip instance named ballAnim exists on the Main Timeline, then path’s data type will be movieclip. If ballAnim did not exist, then path’s data type would be undefined.

This data type refers to any code-based objects that you create with ActionScript. For example, we used the Color and Sound Objects to enhance interactive presentations. The following code would be typed as object:

If you used this code in your Flash movie, you would see object types in the Output window when Debug➪List Variables is used in the Test Movie environment:

In Flash 5, you can define your own subroutines of ActionScript code. We discuss subroutines and constructor functions later. The function data type will be assigned to any ActionScript code that begins with the function command, such as:

If you check for the data type of a nonexistent code element, then Flash 5 ActionScript will return a data type of undefined.

Checking data types with typeof
Now that you know the various data types in Flash 5 ActionScript, you’ll want to know how to check the data type of a given piece of information. Using the typeof operator, you can determine the data type of an ActionScript element. The typeof operator accepts only one option: the name of the ActionScript element that you wish to test. For example, you can trace a variable (or object) type in the Output window:

When this movie is tested, the Output window will display: first Name has a data type of string

You can use typeof in for . . . in loops, so that actions will be executed with specific data types. The following ActionScript code will take any string variables on the Main Timeline and move them to an object (or Movie Clip instance) named globalVar:

The preceding code block will move all variables except the native $version variable to the Movie Clip instance (or object) named globalVar.

Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Flash Topics