Flylib.com

Books Software

 
 
 

Properties in the Real World

 <  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 ( term ). These are properties of a loan, and they describe a loan pretty thoroughly. Everywhere we look in the real world, the objects we interact with have properties. Even the book you are reading now has properties, as shown in Table 3.1.

Table 3.1. Properties of This Book

P ROPERTY

V ALUE

Authors

Tapper, Talbot, Haffner

Publisher

New Riders

Chapters

17

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 Objects

Just 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.

graphics/03fig01.gif

Some of these properties are familiar, indicating positioning on the Stage ( _x , _y ), size (_height , _width , _xscale , and _yscale ), and information about frames (_ totalFrames , _ framesLoaded , and _ currentFrame ). Each property describes the unique characteristics of an instance of a movie clip.

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 variables on the main timeline. Figure 3.2 shows a simple use of the properties of TextField instances to facilitate simple addition.

Figure 3.2. Using the text property of TextField instances enables you to read and write to them.

graphics/03fig02.gif

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 user 's input from a text field, we refer to the text property of that field. In addition, to assign text to a text field, we also use the same text property.

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.

graphics/03fig03.gif

 <  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 numbers ; instead, it is concatenating them. In ActionScript 2.0, as well as several other languages, the + operator is overloaded . This means it will perform different operations, based on the types of data on which it is acting; when acting on numbers, it will add them, when acting on strings, it will concatenate them.

N OTE

As is standard in programming parlance, concatenating is the act of joining two strings to make a new single string. For more details on operators in ActionScript, see Macromedia Flash MX 2004 Advanced Training from the Source , by Derek Franklin and Jobe Makar.


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 specifically instructed to do so. The text property of a text field is typed as a String object. Therefore, when Flash was instructed to show the results of the following:

input1.text + input2.text;

it took the string entered in the first box and concatenated it with the string in the second. If we truly want Flash to add the numbers instead of concatenating them, we will need to cast them to the proper datatype. Casting is how we instruct Flash to change the datatype of an object. If we want to cast a string as a number, we use the built-in Number() function. Equally, if we wanted to cast a number as a String object, we could use the String() function. Listing 3.1 shows the onRelease handler for the button rewritten to properly add the numbers that have been input.

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.

graphics/03fig04.jpg

N OTE

The first four lines of ActionScript 2.0 shown in Figure 3.4 are declaring the datatypes of the visual objects on the Stage. Although this is optional, it helps the Flash compiler force the datatype checking within the application, and it also enables code hinting.


 <  Day Day Up  >