Data types, by themselves, are exactly what their name implies: types of data. For many reasons, it is helpful to be able to distinguish one kind of data from another when running a script. For example, to avoid errors, it would be helpful to know that you were performing a mathematical operation on a string if you thought the data you were manipulating was really a number. Flash recognizes many data types. Some are quite general programming types, such as Number (a numeric value), String (text, or a string of characters), and Boolean (a true or false value, which you read about when learning about conditionals in Chapter 7). Still others are very Flash-specific. For example, when you refer to a movie clip in a script, it too has a data type: MovieClip. There are also data types such as Sound and Video. Many ActionScript classes, including MovieClipLoader and LoadVars (both of which you will use in this book), have associated data types, too. If most of the focus on data types is behind the scenes, as it has been so far in this book, you don't have to worry about it. The original version of ActionScript, now referred to as ActionScript 1.0, automatically cast data into the correct type whenever possible. This is known as dynamic typing because the data types are dynamically reassigned. The downside to this approach, however, is that you aren't warned when this occurs, so you have no way of knowing it's happened. Consequently, it's harder to know how to find a bug in your code. ActionScript 2.0, first introduced in Flash MX 2004, brought true object-oriented programming to Flash. ActionScript 2.0 is not an entirely new language, but it is much more mature and capable than its predecessor. In some ways, it is more similar to robust low-level languages such as Java. The object-oriented aspects of ActionScript 2.0 are beyond the scope of this text. However, you will look at another important concept ActionScript 2.0 introduced, which does not rely on object-oriented techniques: static typing. Unlike the dynamic typing of ActionScript 1.0, static typing requires that you declare what kind of data will be stored in a variable, for example, and will not automatically cast to another type. This means an extra step or two when writing your scripts, but it has the huge advantage of warning you about data type mismatches when you compile your code. Right away, you know there is an error and where it is. If you can determine why a mathematical operation is being applied to a string, for example, you will be able to squash your bugs faster and more effectively. The data type of a variable is specified using post-colon syntax. That is, the var keyword begins a variable declaration statement, and the data type immediately follows a colon after the variable name. This can be done without any further assignment, as a way of declaring your variables and data types before scripting, or along with an assignment for brevity. Examples of both techniques follow: var myNum:Number; var myString:String = "Flash"; When this syntax is used, the ActionScript 2.0 compiler can warn you if you make a mistake related to data types. For instance, it can display a type mismatch error warning when data isn't of the expected type. If you try the following, you will be warned of a type mismatch because you are trying to assign a string to a number: var myNum:Number = "Flash"; Similarly, the compiler can warn you when you try to use an unsupported method or property. For example, it should make sense that you can't specify an x-coordinate for a sound. Without visual data, you can't position a sound on the Stage. Therefore, if you try to set the _x movie clip property for the Sound class (which you'll learn about in the section " Scripting Your Own Sound Control"), you will get a warning that no such property exists: var mySound:Sound; mySound._x = 10; It's also a good idea to type the data passed into a function and returned from a function. Argument typing is similar to variable typing, but the var keyword is not required. Typing a value returned by a function is a bit different. This typing statement is placed just after the function constructor. The following example tells the compiler that a number should be passed into the argument, and that a number should also be returned by the function: function timesThree(whichNum:Number):Number { return whichNum * 3; } When a value is not returned by the function, using a data type of void is recommended. This tells the compiler to alert you if anything unexpected is returned: function timesThree(whichNum:Number):Void { trace(whichNum * 3); } Finally, for completeness's sake, it is usually recommended that anonymous functions, even without arguments, declare no return by using the void data type: my_btn.onRelease = function():Void { my_mc.play(); }; You will find that this is frequently omitted, simply because the scripter knows no value is returned. However, it is still recommended. Type checking is meant to help you with compile-time error reports. Omitting type declarations in ActionScript 2.0 will not prevent your file from compiling, but you won't receive any notifications of possible errors, either. You may be asking yourself why, when you didn't make type declarations in the prior chapter examples, you didn't receive errors. There are two reasons. First, when you're learning a new topic, it's helpful to learn it in digestible pieces. This is another example of chunking information. Second, and more importantly, current Flash Players must remain compatible with older projects. If a data type is not specified for a variable or object, the compiler won't perform type checking. This allows older code to go through the ActionScript 2.0 compiler without generating errors. Although backward compatibility allows untyped objects to be compiled without error, it is considered a best practice to use static typing. All of the examples in the last half of this book will follow this guideline. Information about additional issues relevant to static typing, such as using the Object data type when more than one kind of data may pass through an object and circumventing type checking altogether, can be found in Colin Moock's ActionScript: The Definitive Guide (O'Reilly). |