| < Day Day Up > |
Properties in the Real World
A key goal of object-oriented programming (OOP) is to design the objects for the system to closely mirror the characteristics and behaviors of the real-world objects they represent. The
Loan
example shows this well; loans have three properties inherent to them: the amount being borrowed (principal), the interest (rate), and the time that will be taken to repay the loan (
Table 3.1. Properties of This Book
Hopefully, this illustrates that properties aren't really that foreign of a concept; everything has properties, even you! |
| < Day Day Up > |
| < Day Day Up > |
Properties of Familiar Macromedia Flash MX 2004 ObjectsJust as all real-world objects can be described by their properties, the same holds true for internal Flash objects. Figure 3.1 shows the properties for a movie clip. Figure 3.1. The properties of a movie clip.
Some of these properties are familiar, indicating positioning on the Stage (
_x
,
_y
),
We can also find similar information about the properties of any of the built-in objects in Flash. For example, an instance of the Array class has a property describing the number of elements within the array ( length ), a TextField instance has a property that describes the text currently within it ( text ), and an instance of the Sound class has a property to describe how long the sound lasts ( duration ).
Working with properties of an object is as simple as working with
Figure 3.2. Using the text property of TextField instances enables you to read and write to them.
On the Stage are three text fields and a button. The top two text fields (
input1
and
input2
) are input text; the bottom is a dynamic text field (
total
). An
onRelease
event is added for the button instance (
btAdd
). When clicked and released, the text properties of the two input fields are added, and the results are shown in the total field. We can see that to read a
Note that this simple example of the use of properties within Flash is flawed. Figure 3.3 shows what happens if we run the file and attempt to use it to add 2 and 3. Figure 3.3. The Addition tool shows some unexpected results.
|
| < Day Day Up > |
| < Day Day Up > |
Strong Datatypes for Properties
The flaw we find in Figure 3.3 is that it is not adding the two
As we learned in Chapter 1, "What's New in ActionScript 2.0?," ActionScript 2.0 is a strongly typed language. This means that when Flash determines what type of data a variable (or property) will hold, it won't accept other types of data, unless
input1.text + input2.text;
it took the string entered in the first box and
Listing 3.1. The Addition Tool Now Shows the Expected Results
btAdd.onRelease = function(){
var theTotal:Number = Number(input1.text)+Number(input2.text);
total.text = String(theTotal);
}
Here, we can see that we are explicitly casting the input from the text fields to numbers so that Flash can add them properly. We also need to cast the resulting number back to a string, as the text property of a text field is built to accept only strings. Figure 3.4 shows the full code, Stage, and results with the proper casting. Figure 3.4. The Addition tool working properly with the help of casting.
|
| < Day Day Up > |